单活动中的Android搜索活动

12

我正在尝试让我的应用程序只包含一个活动。这个活动应该能够创建搜索并接收搜索。不幸的是,当我点击操作栏中的搜索按钮时,我的SearchView会出现“双重”搜索栏。我的意思是,在操作栏中会出现一个黑色的搜索栏(SearchView),然后第二个白色的搜索栏会出现在操作栏上方。有什么帮助吗?我做错了什么吗? 对不起,这个搜索东西对我来说都很新和令人困惑。

MainActivity(唯一的活动):

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>

可搜索的.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" />

在清单文件中:

<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" />
4个回答

26

来自Android文档中的设置搜索界面

在可搜索的活动中,通过在onCreate()方法中检查该活动是否有ACTION_SEARCH意图来处理它。

注意:如果你的可搜索活动以single top模式启动(android:launchMode="singleTop"),还需要在onNewIntent()方法中处理ACTION_SEARCH意图。在single top模式下,只会创建一个活动实例,并且后续调用启动该活动的操作不会在栈上创建新的活动实例。这种启动模式非常有用,因为用户可以在同一活动中执行搜索操作,而不必每次都创建一个新的活动实例。


2
那么提问者做错了什么?他在onNewIntent()中也有处理。 - AlikElzin-kilaka

16

谢谢,我更新了我的清单。那不是我的问题,但知道这点真的很好。请查看我的答案,了解我遇到的问题的解决方案。 - Eric Cochran
1
请记得使用onNewIntent()方法来处理您的搜索查询,正如Phil所解释的那样。 - djondal
拥有多个意图过滤器绝对没有问题。 - Pedro Lobito

3

好的,问题与在onOptionsItemSelected(MenuItem item)中调用onSearchRequested()有关。如果我有一个SearchView,则这是多余的,并且只应在旧平台上调用。

因此,我为Honeycomb以下设备创建了单独的菜单项。它在新设备上运行时被删除。针对旧设备,在运行时删除SearchView。

请参见下面的更新代码:

@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) {
        //remove old
        menu.removeItem(R.id.search_old);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    } else{
        //remove new
        menu.removeItem(R.id.search);
    }


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search_old:
            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();
}

1

谢谢,这对我有用。

清单:

        <activity
        android:name=".Buscar"
        android:configChanges="orientation|screenSize"
        android:label="@string/title_activity_buscar"
        android:launchMode="singleTop">

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

活动:

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // getIntent() should always return the most recent
    setIntent(intent);

    query = intent.getStringExtra(SearchManager.QUERY);
    mysearch(query);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接