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

标签 android

我正在尝试实现搜索对话框,但我无法显示来自 Activity 的搜索。

我在我的 list 文件中定义了我的主要 Activity ,这个 Activity 向用户展示了他们必须从中选择的选项列表。其中一个选项是搜索选项。

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

我的搜索 Activity 在我的 list 文件中定义如下。

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

现在我的问题是当我从 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));  
          }  
      }  

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

最佳答案

你检查过你的 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>

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

此外,无需在调用 Activity 中调用或重写 onSearchRequested() 方法,除非您想从您的 UI 小部件之一(如 Button 或 ListItem)调用搜索。

关于android - 如何使用 onSearchRequested() 调用搜索对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4465529/

相关文章:

android - 如何通过android中的 Assets 文件夹共享图像?

Android WebView 没有视频只有音频

android - RecyclerView 适配器数据在调用 notifyitemsetchanged() 后重置

android - 如何显示下一张/上一张卡片的一部分 RecyclerView

Android bool 资源未被识别为 bool 值,而是识别为 int

java - 无法下载 html

android - 将数据同步到 Google 帐户有哪些限制?

android - 微调器数据内容不可见

android - 错误:链接文件资源失败。没有任何错误信息

android - 如何将数据从android发送到用java制作的google app engine web应用程序