当前位置: 移动技术网 > IT编程>移动开发>Android > Android ActionBar搜索功能用法详解

Android ActionBar搜索功能用法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

腾讯电话客服,死神同人之御神,台服痞子狼

本文实例讲述了android actionbar搜索功能用法。分享给大家供大家参考,具体如下:

使用actionbar searchview时的注意点:

首先要吐槽一下 ,关于用法讲得不明确,可能是一直没更新的原因吧。

本来照着文档搞了一下,hint死活出不来,也无法跳转到搜索结果activity。

stackoverflow也有人提出了这个问题,答案说得很明白 - 。

正确用法

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()));

注意点

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" />

另一种方法

既然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.

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android操作xml数据技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网