这很简单。您只需要使用Google的搜索功能,以便当用户长按菜单按钮时,它会加载一个搜索文本框,您可以在其中输入搜索查询以在您的应用程序中进行搜索。
可搜索的应用程序文件必须包含该元素作为根节点并指定一个或多个属性。 e.g
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
然后宣布任何活动接受ACTION_SEARCH即
<application ... >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
现在,我们需要控制查询以及它是如何执行。 我用这个为我的应用程序。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
android androidsearch
好吧,但如果我使用ListFragment,那么我将不必使用SearchView。我可以使用EditText和OnKeyListener来获得相同的效果。问题是,如果有一种方法可以挂钩到SearchView并使用其建议机制和UI,但是可以通过自定义的方式从我想要的任何源获取建议,而不一定是内容提供者。 – user940016
只需制作您自己的ContentProvider即可返回您想要的数据(不一定是数据库)。 这或子类SearchView并重新编写适当的方法接受来自你想要的数据并适当地显示它(你可能需要看看源代码,看看它是如何完成的) – bennettaur
好吧,但那么怎么样可搜索的活动?我不想使用它,但我必须定义它。我可以建立一个虚拟活动,但并不真正显示吗? – user940016