通过 Searchable xml 配置设置 Android 搜索小部件选项
我有一个菜单项使用搜索小部件作为其 actionViewClass
,如下所示:
<item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
它工作正常,但是我似乎在 android 网站文档中提到了可搜索的 xml 配置文件,如下所示:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="string resource"
android:hint="string resource" />
我怎样才能获取菜单项搜索小部件以使用上述配置?我认为这与 SearchManager.setSearchableInfo
方法有关,我在代码中获取了搜索小部件的句柄,如下所示:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.setSearchableInfo(searchManager.getSearchableInfo(R.layout.searchable));
如您所见,我想使用 < code>getSearchableInfo 方法并传入我的可搜索配置文件的 id。
有人可以帮我吗?
I have a menu item uses the search widget as its actionViewClass
that looks like so:
<item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
It works fine, however I have seem mention on the android site docs of a searchable xml config file like this:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="string resource"
android:hint="string resource" />
How can I get the menu item search widget to use the above configuration?? I assume it is something to do with the SearchManager.setSearchableInfo
method, I am getting a handle to the search widget in my code like this:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.setSearchableInfo(searchManager.getSearchableInfo(R.layout.searchable));
As you can see I want to do something like above with the getSearchableInfo
method and pass in the id of my searchable config file.
Can anyone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建一个SearchActivity,它处理来自搜索小部件的搜索查询并显示搜索结果,并且您还需要在android清单文件中声明它。
搜索活动的示例:
在清单中:
您需要将搜索配置文件“searchable.xml”放入文件夹 res/xml 中。
searchable.xml 文件如下所示:
为了提供自定义搜索建议,您需要创建一个扩展 ContentProvider 或 SearchRecentSuggestionsProvider 的类。 ,请参阅此处
有关更多详细信息 在搜索视图中,您需要将可搜索信息设置为:
希望这对您有帮助,祝您好运!
You need to create a SearchActivity, which process the search queries from the search widget and show the search results and also you need to declare it in the android manifest file.
An example of search activity:
and in the manifest:
You need to put the search configuration file 'searchable.xml' in the folder res/xml .
The searchable.xml file looks like this:
And to provide custom search suggestions, you need to create a class which extends ContentProvider or SearchRecentSuggestionsProvider. For more details look here
Also in the activity which contains the search view, you need to set the searchable info as:
Hope this helps you, Good luck!