【问题标题】:Start new activity from SearchView从 SearchView 开始新活动
【发布时间】:2015-06-14 22:48:20
【问题描述】:

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

机器人清单:

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

搜索活动

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

结果活动:

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

    }
...
}

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

【问题讨论】:

  • 所以我假设您有数据要搜索,对吧?您是否还在 Activity 中实现了任何接口?

标签: android searchview


【解决方案1】:

这个答案有点晚了,但我觉得它对未来的观众很有用。困境似乎来自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标签。因此,我们的活动在 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>

资源:

【讨论】:

  • 精彩演示
  • 我按照每一步,但搜索结果仍然转到当前活动,SearchResultsActivity onCreate() 没有被调用,有人有同样的问题吗?
  • 我发现了我的问题,因为即使在当前活动中我也听 onQueryTextSubmit(),它返回 true,结果不会提交给 SearchResultsActivity。我唯一需要改变的就是为 onQueryTextSubmit() 返回 false。
  • 很好的解释。谢谢你。它对我真的很有帮助:)
  • 为什么我们需要一个新的组件名称?
【解决方案2】:

在没有看到您的活动代码的情况下,我建议您尝试这种方法 - 同时假设您已按上述方式创建了所有文件;

在您的结果活动中,

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.core_actions, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setIconifiedByDefault(false);
    searchView.setQueryHint(getString(R.string.search_hint));

    searchView.setOnQueryTextListener(this);

    return true;
}

请记住,这是包含您想要搜索的数据的活动:

你必须在同一个activity中实现SearchView.OnQueryTextListener接口:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_search:
            ProductsResulstActivity.this.finish();

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {

    productFilterAdapter.getFilter().filter(newText);

    if (TextUtils.isEmpty(newText)) {
         listView.clearTextFilter();
     }
    else {
       listView.setFilterText(newText);
    }

    return true;
}

productFilterAdapter 是您必须事先创建的适配器。

它应该实现 Filterable 接口。我希望这有帮助。

如果您需要进一步的帮助,请告诉我。祝你好运

【讨论】:

    【解决方案3】:

    我了解我也遇到过同样的问题,这是因为您通过传递当前组件名称来传递

    getComponentName()

    这将由当前活动名称初始化,因此您需要使用以下格式的可搜索活动名称对其进行初始化,并传递启动新活动的相同组件实例。

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
        search.setQueryHint(getResources().getString(R.string.search_hint));
    

    希望我已经回答了这个问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多