单个 Activity 中的 Android 搜索 Activity

标签 android searchview android-searchmanager android-search

我试图让我的应用程序包含一个单一的 Activity 。此 Activity 应该能够创建搜索并接收搜索。不幸的是,当我单击操作栏中的搜索按钮时,我的 SearchView 中出现了一个“双重”搜索栏。我的意思是有一个搜索栏(深色 - SearchView)在操作栏中出现一秒钟,然后第二个(白色)出现在操作栏上方。有什么帮助吗?我究竟做错了什么? 抱歉,这个搜索对我来说是全新的和困惑的。

MainActivity(唯一的 Activity ):

public class MainActivity extends ActionBarActivity {
Menu mMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getSupportActionBar().setDisplayShowTitleEnabled(false);
    setContentView(R.layout.activity_main);

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
    }
}

@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mMenu = menu;
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}

@SuppressLint("NewApi")
@Override
public boolean onSearchRequested() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        MenuItem mi = mMenu.findItem(R.id.search);
        if(mi.isActionViewExpanded()){
            mi.collapseActionView();
        } else{
            mi.expandActionView();
        }
    } else{
        //onOptionsItemSelected(mMenu.findItem(R.id.search));
    }
    return super.onSearchRequested();
}
}

main.xml(菜单 xml):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.brianco.andypedia="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/search"
      android:title="@string/action_settings"
      android:icon="@drawable/ic_launcher"
      android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
      com.brianco.andypedia:showAsAction="always|collapseActionView"
      com.brianco.andypedia:actionViewClass="android.support.v7.widget.SearchView" />

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu>

searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />

在 list 中:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.brianco.andypedia.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
    </activity>

    <meta-data android:name="android.app.default_searchable"
        android:value=".MainActivity" />

最佳答案

来自 Setting Up the Search Interface在 Android 文档中:

In your searchable activity, handle the ACTION_SEARCH intent by checking for it in your onCreate() method.

Note: If your searchable activity launches in single top mode (android:launchMode="singleTop"), also handle the ACTION_SEARCH intent in the onNewIntent() method. In single top mode, only one instance of your activity is created and subsequent calls to start your activity do not create a new activity on the stack. This launch mode is useful so users can perform searches from the same activity without creating a new activity instance every time.

关于单个 Activity 中的 Android 搜索 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308193/

相关文章:

android - 在 Android 中使用 SearchView 进行搜索后,ListView 未更新

java - 尝试在 Eclipse 中设置 Android 开发 - 找不到 SDK 位置?

android - 如何根据其状态设置 SearchView 的样式?

android - 在打开 SearchView 的情况下启动 Activity

android pass bundle with search

android - 如何使用默认浏览器在谷歌上搜索字符串

android - 在 Xamarin Forms 中设置元素的资源名称

android - 如何将 Android-Project 添加到 GitHub

android - android studio中的光标选择矩形区域

android - SearchView 不显示在带有嵌套 fragment 的工具栏上