源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android ActionBar搜索功能用法详解

  • 时间:2020-05-10 18:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android ActionBar搜索功能用法详解
本文实例讲述了Android ActionBar搜索功能用法。分享给大家供大家参考,具体如下: 使用ActionBar SearchView时的注意点: 首先要吐槽一下[url=http://developer.android.com/guide/topics/search/index.html]Android的官方Guide文档[/url] ,关于用法讲得不明确,可能是一直没更新的原因吧。 本来照着文档搞了一下,hint死活出不来,也无法跳转到搜索结果Activity。 StackOverflow也有人提出了这个问题,答案说得很明白 - [url=http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work/11704661#11704661]参考链接[/url]。 [b]正确用法[/b] 1. 在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>

2. 在提供搜索结果的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>

3. @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>

4. 初始化Menu的时候,获取SearchableInfo
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

[b]注意点[/b] SearchManager通过ComponentName查找SearchableInfo的时候,对应Component必须满足一定条件: 1. intent-filter包含
<action android:name="android.intent.action.SEARCH" />
2. meta-data
<meta-data
  android:name="android.app.searchable"
  android:resource="@xml/searchable" />

[b]另一种方法[/b] 既然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));

[b]tips[/b] ComponentName构造函数的第一个参数pkg是Application的Package,不是目标类所在的Package。 The first parameter ComponentName(String pkg, String cls) is application package not the package where the activity is. 更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/837.htm]Android操作XML数据技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/848.htm]Android操作SQLite数据库技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/845.htm]Android操作json格式数据技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/224.htm]Android编程开发之SD卡操作方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部