ActionBar的搜索功能
来源:互联网 发布:excel2010查找重复数据 编辑:程序博客网 时间:2024/04/29 11:37
使用ActionBar SearchView时的注意点:
首先要吐槽一下Android的官方Guide文档,关于用法讲得不明确,可能是一直没更新的原因吧。
本来照着文档搞了一下,hint死活出不来,也无法跳转到搜索结果Activity。
StackOverflow也有人提出了这个问题,答案说得很明白 - 参考链接。
正确用法
在
AndroidManifest.xml
中为提供SearchView
的Activity添加meta-data
<activity android:name=".navigation.NavigationActivity" android:label="@string/title_activity_navigation"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.app.default_searchable" android:value=".search.SearchResultActivity" /></activity>
在提供搜索结果的Activity中添加为
SearchableInfo
用的meta-data
<activity android:name=".search.SearchResultActivity" android:label="@string/title_activity_search_result" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <!--This metadata entry provides further configuration details for searches--> <!--that are handled by this activity.--> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /></activity>
@xml/searchable
文件中的android:hint
只能使用string.xml
中定义的字符串,不能hard-coded<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/search_hint"></searchable>
初始化Menu的时候,获取
SearchableInfo
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
注意点
SearchManager
通过ComponentName
查找SearchableInfo
的时候,对应Component必须满足一定条件:
intent-filter
包含<action android:name="android.intent.action.SEARCH" />
meta-data
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
另一种方法
既然SearchManager
是通过ComponentName
来获取SearchableInfo
,当然可以直接从提供搜索结果的Activity中获取ComponentName
。
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();ComponentName cn = new ComponentName("com.liangfeizc.catykanji", "com.liangfeizc.catykanji.search.SearchResultActivity");searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
tips
ComponentName构造函数的第一个参数pkg是Application的Package,不是目标类所在的Package。
The first parameter ComponentName(String pkg, String cls) is application package not the package where the activity is.
- ActionBar的搜索功能
- 【ActionBar】搜索功能的实现
- actionBar+SearchView实现搜索功能
- ActionBar上添加搜索功能
- ActionBar上添加搜索和分享功能
- ActionBar实用案例(返回功能、子菜单、搜索功能)
- ActionBar 的 Navigation UP 功能
- android ActionBar 兼容低版本之搜索功能
- Android中在ActionBar中添加搜索功能
- ActionBar详解(三)--->ActionBar的Home导航功能
- [android]ActionBar的搜索条使用
- Android UI ActionBar功能-ActionBarProvider的使用
- Android UI ActionBar功能-ActionBarSherlock 的使用
- Lucene的搜索功能
- 搜索功能的实现
- Android actionbar 搜索框
- ActionBar 搜索,tab
- 收起actionbar 上的搜索框 正确写法
- Myeclipse 2014 debug工具条消失还原
- 内存中常见异常值的解释(比如0xcccccccc、0xcdcdcdcd和 0xfeeefeee 异常值 )
- 基础练习4_数列特征
- C#中如何实现 textBox 文本的换行
- Rundll32.exe的小理解
- ActionBar的搜索功能
- 不带头节点的单向链表逆序
- [Java Performance] JVM 线程调优
- 第一个VSTO程序
- vector 3682 To Be an Dream Architect
- 支付宝账号2元一个,5千万信用卡被盗,银行灾难可能来临了
- 使用pickle模块
- Android内存泄露与分析
- jsp格式化时间等数据格式