我正在尝试实现搜索对话框,但我无法显示来自 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/