Android为程序的搜索功能提供了统一的搜索接口,search dialog和search widget,这里介绍search dialog使用。
search dialog 只能为于activity窗口的上方。下面以点击EditText输入框启动search dialog搜索框为例:
效果如下

实现步骤:

1. 新建searchable.xml配置文件,放在res/xml目录下。
searchable.xml用于配置搜索框相关属性,配置文件内容为:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:label="@string/app_name"
  4. android:hint="@string/search_hint"/>

注:android:lable是唯一必须定义的属性。它指向一个字符串,是应用程序的名字。
实际上该label也只有在search suggestions for Quick Search Box可用时才可见。
android:hint属性不是必须,但是还是推荐总是定义它。它是search box用户输入前输入框中的提示语。

其它属性可以查看google官方文档:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:label="string resource"
  4. android:hint="string resource"
  5. android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
  6. android:searchButtonText="string resource"
  7. android:inputType="inputType"
  8. android:imeOptions="imeOptions"
  9. android:searchSuggestAuthority="string"
  10. android:searchSuggestPath="string"
  11. android:searchSuggestSelection="string"
  12. android:searchSuggestIntentAction="string"
  13. android:searchSuggestIntentData="string"
  14. android:searchSuggestThreshold="int"
  15. android:includeInGlobalSearch=["true" | "false"]
  16. android:searchSettingsDescription="string resource"
  17. android:queryAfterZeroResults=["true" | "false"]
  18. android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
  19. android:voiceLanguageModel=["free-form" | "web_search"]
  20. android:voicePromptText="string resource"
  21. android:voiceLanguage="string"
  22. android:voiceMaxResults="int"
  23. >
  24. <actionkey
  25. android:keycode="KEYCODE"
  26. android:queryActionMsg="string"
  27. android:suggestActionMsg="string"
  28. android:suggestActionMsgColumn="string" >
  29. </searchable>

2. 在AndroidManifest.xml文件中声明Searchable Activity。
Searchable Activity为搜索结果显示Activity,可以定义为搜索框所在的当前Activity,也可以单独定义一个Activity
这里直接定义当前搜索框所在SearchActivity.配置如下:

  1.  
  2. <application
  3. android:allowBackup="true"
  4. android:icon="@mipmap/ic_launcher"
  5. android:label="@string/app_name"
  6. android:supportsRtl="true"
  7. android:theme="@style/AppTheme">
  8. <meta-data
  9. android:name="android.app.default_searchable"
  10. android:value=".SearchActivity"/>
  11.  
  12. <activity android:name=".SearchActivity">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. <intent-filter>
  18. <action android:name="android.intent.action.SEARCH" />
  19. </intent-filter>
  20.  
  21. <meta-data
  22. android:name="android.app.searchable"
  23. android:resource="@xml/searchable" />
  24. </activity>
  25.  
  26. </application>

注:activity标签内的标签必须包括android:name这个属性,而且其值必须为”android.app.searchable”,
还必须包括android:resource这个属性,它指定了我们的search dialog的配置文件。(res/xml/searchable.xml).

3.启动搜索框search dailog:
在activity中调用onSearchRequested()方法

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.search_edit:
  5. onSearchRequested();
  6. break;
  7. }
  8. }

4. 获取搜索关键字
搜索框中输入的搜索关键字通过下面代码可以取到:
String query = intent.getStringExtra(SearchManager.QUERY);
但在获取前应该先判断Intent中action是否为搜索action:”android.intent.action.SEARCH”

  1. Intent intent = getIntent();
  2. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  3. String query = intent.getStringExtra(SearchManager.QUERY);
  4.  
  5. }

5.搜索结果处理方式
在前面已经提过,搜索结果可以在单独一个类里处理,也可以在当前搜索框所在类处理,如果在当前搜索框所在类处理,需设置当前类为SingTop模式,防止再次创建Activity. 但这样又会引发一个问题,搜索时onCreate方法不会在执行,而在可以执行的onResult方法中得到的Intent不包含搜索Action:”android.intent.action.SEARCH”,而是原来的Action。
这说明搜索执行后Intent已经被改变,Activity中通过getIntent()取到的Intent还是原来的Intent。那么被改变的Intent从那里获取呢?
重写 onNewIntent(Intent intent) 获取,执行 setIntent(intent) 更新Activity中Intent,

  1. @Override
  2. protected void onNewIntent(Intent intent) {
  3. setIntent(intent);
  4. }
  5.  
  6. @Override
  7. protected void onResume() {
  8. super.onResume();
  9. if (getIntent().ACTION_SEARCH.equals(getIntent().getAction())) {
  10. String query = intent.getStringExtra(SearchManager.QUERY);
  11. }
  12. }

综上在当前搜索框所在类获取搜索关键字处理搜索结果可以这样写:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_search_dialog_layout);
  5. mSearchEdit = (EditText) findViewById(R.id.search_edit);
  6. mContentTxt = (TextView)findViewById(R.id.search_content_txt);
  7. mSearchEdit.setOnClickListener(this);
  8. handleIntent(getIntent());
  9. }
  10.  
  11. /**
  12. * 重复刷新当前Activity时执行
  13. * @param intent
  14. */
  15. @Override
  16. protected void onNewIntent(Intent intent) {
  17. setIntent(intent);
  18. handleIntent(intent);
  19. }
  20.  
  21. private void handleIntent(Intent intent) {
  22. if (getIntent().ACTION_SEARCH.equals(getIntent().getAction())) {
  23. String query = intent.getStringExtra(SearchManager.QUERY);
  24. doMySearch(query);
  25. }
  26. }
  27. /**
  28. * 处理搜索结果
  29. */
  30. public void doMySearch(String query){
  31. mContentTxt.setText(query);
  32. }

Android 浮动搜索框 searchable 使用(转)。的更多相关文章

  1. android浮动搜索框

    android浮动搜索框的配置比较繁琐,需要配置好xml文件才能实现onSearchRequest()方法. 1.配置搜索的XML配置文件​,新建文件searchable.xml,保存在res/xml ...

  2. Android 系统搜索框(有浏览记录)

    实现Android 系统搜索框(有浏览记录),先看下效果: 一.配置搜索描述文件  要在res中的xml文件加创建sreachable.xml,内容如下: <?xml version=" ...

  3. Android actionbar 搜索框

    就是实如今顶部这种搜索框. 一.这个搜索框是actionbar上的menu上的一个item.叫SearchView.我们能够先在menu选项里定义好: bmap_menu.xml: <?xml ...

  4. (转)Android SearchView 搜索框

    如果对这个效果感觉不错, 请往下看. 背景: 天气预报app, 本地数据库存储70个大中城市的基本信息, 根据用户输入的或通过搜索框选取的城市, 点击查询按钮后, 异步请求国家气象局数据, 得到返回的 ...

  5. Android学习笔记_79_ Android 使用 搜索框

    1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...

  6. Android的搜索框SearchView的用法-android学习之旅(三十九)

    SearchView简介 SearchView是搜索框组件,他可以让用户搜索文字,然后显示.' 代码示例 这个示例加了衣蛾ListView用于为SearchView增加自动补全的功能. package ...

  7. Xamarin.Android 制作搜索框

    前段时间仿QQ做了一个搜索框样式,个人认为还不错,留在这里给大家做个参考,希望能帮助到有需要的人. 首先上截图(图1:项目中的样式,图2:demo样式): 不多说直接上代码: Main.axml &l ...

  8. 详细解读Android中的搜索框—— SearchView

    以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...

  9. 十七、Android学习笔记_Android 使用 搜索框

    1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...

随机推荐

  1. Linux vi 中移动光标 命令

    移动光标 上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以单词向前移动 nw:光标向 ...

  2. Promise A 规范的一个简单的浏览器端实现

    简单的实现了一个promise 的规范,留着接下来模块使用.感觉还有很多能优化的地方,有时间看看源码,或者其他大神的代码 主要是Then 函数.回调有点绕人. !(function(win) { fu ...

  3. HDU5730 FFT+CDQ分治

    题意:dp[n] = ∑ ( dp[n-i]*a[i] )+a[n], ( 1 <= i < n) cdq分治. 计算出dp[l ~ mid]后,dp[l ~ mid]与a[1 ~ r-l ...

  4. Git学习(2)Git 安装

    Windows 平台上安装 在 Windows 平台上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行: 安装包下载地址 ...

  5. wireshark使用教程

    Wireshark: https://www.wireshark.org/ 安装: apt-get install wireshark 教程: http://blog.csdn.net/leichel ...

  6. 基础2 JVM

    1. 内存模型以及分区,需要详细到每个区放什么. //运行时数据区域 方法区 Method Area 各个线程共享的内存区域 存储已被虚拟机加载的类信息 常量 静态变量 即时编译器编译后的代码 虚拟机 ...

  7. [js] 非常好的面试题,学习了。。

    原文链接:http://www.cnblogs.com/xxcanghai/p/5189353.html

  8. 转:c的回归-云风

    C 的回归 周末出差,去另一个城市给公司的一个项目解决点问题.回程去机场的路上,我用手机上 google reader 打发时间.第一眼就看到孟岩大大新的一篇:Linux之父话糙理不糙 .主题是 C ...

  9. python把str转换为int

    def str2int(s): def fn(x,y): return x+y def char2num(s): ':9}[s] return reduce(fn,map(char2num,s)) ' ...

  10. python语法笔记(四)

    1.对象的属性     python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统     对象的属性可能来自于其类定义,叫做类属性:还可能 ...