从开始新的搜索查看活动 [英] Start new activity from SearchView

查看:156
本文介绍了从开始新的搜索查看活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个活动:第一个与搜索视图一个操作栏,第二个应该显示搜索查询的结果。

I have 2 activities: the first has a action bar with a search view, the second should display the results of the search query.

androidmanifest:

androidmanifest:

    <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>

searchable.xml

searchable.xml

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

SearchActivity

SearchActivity

....
@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;
}
....

ResultsActivity:

ResultsActivity:

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

    }
...
}

问题是,输入到搜索查看查询之后,没有任何反应。没有错误,没有什么。我怎样才能打开resultsactivity在searchactivity输入查询后?

the problem is that after a query is entered into the searchview, nothing happens. No errors, nothing. How can i open the resultsactivity after the query is entered in the searchactivity?

推荐答案

这答案是晚了一点,但我觉得它会为未来的观众是有用的。困境似乎来自的Andr​​oid搜索查看教程的不确定性。中的场景,他们盖假定你将在搜索查看所在的同一个活动来显示结果。在这种情况下,在活动标记的Andr​​oidManifest.xml 文件看起来是这样的:

This answer is a little late but I feel it'll be useful for future viewers. The dilemma seems to come from the ambiguity of the Android SearchView tutorial. The scenario they cover assumes you will be displaying the results in the same Activity the SearchView resides. In such a scenario, the Activity tag in the AndroidManifest.xml file would look something like this:

<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>

然后,要处理同一活动的结果,你会重写 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
    }  
}

不过,在这里我们要显示另一个活动结果的情况下,我们必须把意图过滤器和meta标签到结果的活动,并介绍了搜索查看活动一个新的meta标签。所以,我们的活动将是这个样子的的Andr​​oidManifest.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方法,激活搜索查看(假设你要添加的搜索查看到动作条)。而不是使用 getComponentName()在SearchManager的的getSearchableInfo()方法调用,我们实例使用在MainActivity的上下文和SearchResultsActivity类的新组件名对象:

Then, in our MainActivity's onCreateOptionsMenu method, activate the SearchView (assumes you're adding the SearchView to the ActionBar). Rather than using getComponentName() in the SearchManager's getSearchableInfo() method call, we instantiate a new ComponentName object using the MainActivity's context and the SearchResultsActivity class:

@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方法,我们可以处理搜索结果:

Finally, in our SearchResultsActivity class, in the onCreate method, we can handle the search results:

@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 资源文件和搜索查看添加到您的布局。

Don't forget to create the searchable.xml resource file and add the SearchView to your layout.

searchable.xml (RES / XML / searchable.xml;如果需要在资源创建XML文件夹):

searchable.xml (res/xml/searchable.xml; create xml folder under res if needed):

<?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"/>

布局(添加搜索查看到动作条作为菜单项的例子):

Layout (example of adding the SearchView to ActionBar as a menu item):

<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>

资源:

  • Display Results In Same Activity
  • Display Results In Different Activity
  • ComponentName

这篇关于从开始新的搜索查看活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆