如何使用onSearchRequested()调用搜索对话框

use*_*255 8 android

我正在尝试实现搜索对话框,但我无法显示活动中的搜索.

我在我的清单文件中定义了我的主要活动,此活动向用户显示他们必须选择的选项列表.其中一个选项是"搜索"选项.

    <activity
        android:name=".MenuListActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

我的搜索活动在我的清单文件中定义,如此.

<activity android:name=".SearchActivity"  
              android:launchMode="singleTop"  
              android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>  
            <meta-data android:name="android.app.searchable"  
                       android:resource="@xml/searchable"/>  
</activity>
Run Code Online (Sandbox Code Playgroud)

现在我的问题是当我从我的MenuListActivity调用onSearchRequested()时没有任何反应.

public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {  
          String strText = items[position];  

          if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_browse))) {  
              startActivity(new Intent(MenuListActivity.this, WSMobileActivity.class));  
          } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_search))) { 

            // If i call it like this it doesn't work, nothing happens  
              onSearchRequested();  

          } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_locator))) {
              startActivity(new Intent(MenuListActivity.this, StoreLocatorActivity.class));  
          }  
      }  
Run Code Online (Sandbox Code Playgroud)

请帮忙,如何从我的MenuActivity调用搜索请求?

mot*_*ver 25

你检查了你的res/xml/searchable.xml吗?

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:hint="@string/search_hint" 
  android:label="@string/search_label">
</searchable>
Run Code Online (Sandbox Code Playgroud)

当您具有提示和标签的硬编码字符串时,搜索对话框不会显示.它们必须是@string资源.

此外,除非要从一个UI小部件(如Button或ListItem)调用搜索,否则无需在调用活动中调用或覆盖onSearchRequested()方法.


Man*_*hot 3

除了在清单文件中声明 SearchActivity 之外,您还需要包含元数据信息。

如果您想在整个应用程序中调用搜索对话框,请包括

<meta-data android:name="android.app.default_searchable"
               android:value=".SearchActivity" />
Run Code Online (Sandbox Code Playgroud)

在应用程序标签内。

如果您只想在特定活动中调用搜索对话框,则包括

<meta-data android:name="android.app.default_searchable"
               android:value=".SearchActivity" />
Run Code Online (Sandbox Code Playgroud)

在活动标签内。

有关更多详细信息,请参阅 http://developer.android.com/guide/topics/search/search-dialog.html