android - 从 SearchView 开始新 Activity

标签 android searchview

我有 2 个 Activity :第一个有一个带有搜索 View 的操作栏,第二个应该显示搜索查询的结果。

机器人 list :

    <activity
        android:name=".SearchActivity"

        ...
        android:launchMode="singleTop">           

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        ...

    </activity>

   <activity
        android:name=".ResultsActivity"
        ...

         <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

    </activity>

可搜索的.xml

<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/enter_a_word" />

搜索 Activity

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

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =  (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));

    return true;
}
....

结果 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);

    }
...
}

问题是在搜索 View 中输入查询后,什么也没有发生。没有错误,什么都没有。在 searchactivity 中输入查询后如何打开 resultsactivity?

最佳答案

这个答案有点晚了,但我觉得它对 future 的观众很有用。困境似乎来自Android SearchView tutorial. 的模棱两可。他们涵盖的场景假设您将在 SearchView 所在的同一个 Activity 中显示结果。在这种情况下,AndroidManifest.xml 文件中的 Activity 标记将如下所示:

<activity
    android:name=".MainActivity"
    android:label="@string/main_activity_label"
    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>

然后,要在同一个 Activity 中处理结果,您将重写 onNewIntent 方法:

@Override
public void onNewIntent(Intent intent){
    setIntent(intent);
    if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //now you can display the results
    }  
}

但是,在我们想要在另一个Activity中显示结果的情况下,我们必须将Intent Filter和meta标签放入结果Activity中,并为SearchView Activity引入一个新的meta标签。因此,我们的 Activity 在 AndroidManifest.xml 文件中将如下所示:

<activity
        android:name=".MainActivity"
        android:label="@string/main_activity_label"
        android:launchMode="singleTop">
        <!-- meta tag points to the activity which displays the results -->
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
</activity>
<activity
        android:name=".SearchResultsActivity"
        android:label="@string/results_activity_label"
        android:parentActivityName="com.example.MainActivity">
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
        <!-- meta tag and intent filter go into results activity -->
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
</activity>

然后,在 MainActivity 的 onCreateOptionsMenu 方法中,激活 SearchView(假设您将 SearchView 添加到 ActionBar)。我们不是在 SearchManager 的 getSearchableInfo() 方法调用中使用 getComponentName(),而是使用 MainActivity 的上下文和 SearchResultsActivity 类实例化一个新的 ComponentName 对象:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
    search.setQueryHint(getResources().getString(R.string.search_hint));
    return true;
}

最后,在我们的 SearchResultsActivity 类中,在 onCreate 方法中,我们可以处理搜索结果:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
    }
}

不要忘记创建 searchable.xml 资源文件并将 SearchView 添加到您的布局中。

searchable.xml(res/xml/searchable.xml;如果需要,在res下创建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"/>

布局(将 SearchView 作为菜单项添加到 ActionBar 的示例):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.MainActivity">
    <group android:checkableBehavior="single">
        <item android:id="@+id/action_search" android:title="Search"
            android:orderInCategory="1" app:showAsAction="collapseActionView|ifRoom"
            app:actionViewClass="android.support.v7.widget.SearchView"/>
    </group>
</menu>

资源:

关于android - 从 SearchView 开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29540724/

相关文章:

android - 如何将 Timber 日志重定向到 Junit logcat

android - 用 Android 写 "NfcA only"标签

java - 尝试将 amazon mturk SDK 实现到 android studio 项目时出错

android - Intent.ACTION_PICK 崩溃,类型为 "audio/*"

android - 有没有更好的方法来恢复 SearchView 状态?

android - 在操作栏中设置 android 搜索 View 和下拉列表的样式

android - SearchView 不搜索

android - 与字符串匹配并在android中使用本地数据库检索所有可用字符串

android - 使用 android.support.v7.widget.SearchView 在 LinearLayout 中搜索 View

android - 在 Android 搜索 View 中显示建议列表中的选择