Android 浮动搜索框 searchable 使用(转)。
Android为程序的搜索功能提供了统一的搜索接口,search dialog和search widget,这里介绍search dialog使用。
search dialog 只能为于activity窗口的上方。下面以点击EditText输入框启动search dialog搜索框为例:
效果如下
实现步骤:
1. 新建searchable.xml配置文件,放在res/xml目录下。
searchable.xml用于配置搜索框相关属性,配置文件内容为:
- <?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"/>
注:android:lable是唯一必须定义的属性。它指向一个字符串,是应用程序的名字。
实际上该label也只有在search suggestions for Quick Search Box可用时才可见。
android:hint属性不是必须,但是还是推荐总是定义它。它是search box用户输入前输入框中的提示语。
其它属性可以查看google官方文档:
- <?xml version="1.0" encoding="utf-8"?>
- <searchable xmlns:android="http://schemas.android.com/apk/res/android"
- android:label="string resource"
- android:hint="string resource"
- android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
- android:searchButtonText="string resource"
- android:inputType="inputType"
- android:imeOptions="imeOptions"
- android:searchSuggestAuthority="string"
- android:searchSuggestPath="string"
- android:searchSuggestSelection="string"
- android:searchSuggestIntentAction="string"
- android:searchSuggestIntentData="string"
- android:searchSuggestThreshold="int"
- android:includeInGlobalSearch=["true" | "false"]
- android:searchSettingsDescription="string resource"
- android:queryAfterZeroResults=["true" | "false"]
- android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
- android:voiceLanguageModel=["free-form" | "web_search"]
- android:voicePromptText="string resource"
- android:voiceLanguage="string"
- android:voiceMaxResults="int"
- >
- <actionkey
- android:keycode="KEYCODE"
- android:queryActionMsg="string"
- android:suggestActionMsg="string"
- android:suggestActionMsgColumn="string" >
- </searchable>
2. 在AndroidManifest.xml文件中声明Searchable Activity。
Searchable Activity为搜索结果显示Activity,可以定义为搜索框所在的当前Activity,也可以单独定义一个Activity
这里直接定义当前搜索框所在SearchActivity.配置如下:
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <meta-data
- android:name="android.app.default_searchable"
- android:value=".SearchActivity"/>
- <activity android:name=".SearchActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <meta-data
- android:name="android.app.searchable"
- android:resource="@xml/searchable" />
- </activity>
- </application>
注:activity标签内的标签必须包括android:name这个属性,而且其值必须为”android.app.searchable”,
还必须包括android:resource这个属性,它指定了我们的search dialog的配置文件。(res/xml/searchable.xml).
3.启动搜索框search dailog:
在activity中调用onSearchRequested()方法
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.search_edit:
- onSearchRequested();
- break;
- }
- }
4. 获取搜索关键字
搜索框中输入的搜索关键字通过下面代码可以取到:
String query = intent.getStringExtra(SearchManager.QUERY);
但在获取前应该先判断Intent中action是否为搜索action:”android.intent.action.SEARCH”
- Intent intent = getIntent();
- if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- }
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,
- @Override
- protected void onNewIntent(Intent intent) {
- setIntent(intent);
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (getIntent().ACTION_SEARCH.equals(getIntent().getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- }
- }
综上在当前搜索框所在类获取搜索关键字处理搜索结果可以这样写:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_search_dialog_layout);
- mSearchEdit = (EditText) findViewById(R.id.search_edit);
- mContentTxt = (TextView)findViewById(R.id.search_content_txt);
- mSearchEdit.setOnClickListener(this);
- handleIntent(getIntent());
- }
- /**
- * 重复刷新当前Activity时执行
- * @param intent
- */
- @Override
- protected void onNewIntent(Intent intent) {
- setIntent(intent);
- handleIntent(intent);
- }
- private void handleIntent(Intent intent) {
- if (getIntent().ACTION_SEARCH.equals(getIntent().getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- doMySearch(query);
- }
- }
- /**
- * 处理搜索结果
- */
- public void doMySearch(String query){
- mContentTxt.setText(query);
- }
Android 浮动搜索框 searchable 使用(转)。的更多相关文章
- android浮动搜索框
android浮动搜索框的配置比较繁琐,需要配置好xml文件才能实现onSearchRequest()方法. 1.配置搜索的XML配置文件,新建文件searchable.xml,保存在res/xml ...
- Android 系统搜索框(有浏览记录)
实现Android 系统搜索框(有浏览记录),先看下效果: 一.配置搜索描述文件 要在res中的xml文件加创建sreachable.xml,内容如下: <?xml version=" ...
- Android actionbar 搜索框
就是实如今顶部这种搜索框. 一.这个搜索框是actionbar上的menu上的一个item.叫SearchView.我们能够先在menu选项里定义好: bmap_menu.xml: <?xml ...
- (转)Android SearchView 搜索框
如果对这个效果感觉不错, 请往下看. 背景: 天气预报app, 本地数据库存储70个大中城市的基本信息, 根据用户输入的或通过搜索框选取的城市, 点击查询按钮后, 异步请求国家气象局数据, 得到返回的 ...
- Android学习笔记_79_ Android 使用 搜索框
1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...
- Android的搜索框SearchView的用法-android学习之旅(三十九)
SearchView简介 SearchView是搜索框组件,他可以让用户搜索文字,然后显示.' 代码示例 这个示例加了衣蛾ListView用于为SearchView增加自动补全的功能. package ...
- Xamarin.Android 制作搜索框
前段时间仿QQ做了一个搜索框样式,个人认为还不错,留在这里给大家做个参考,希望能帮助到有需要的人. 首先上截图(图1:项目中的样式,图2:demo样式): 不多说直接上代码: Main.axml &l ...
- 详细解读Android中的搜索框—— SearchView
以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...
- 十七、Android学习笔记_Android 使用 搜索框
1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...
随机推荐
- Linux vi 中移动光标 命令
移动光标 上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以单词向前移动 nw:光标向 ...
- Promise A 规范的一个简单的浏览器端实现
简单的实现了一个promise 的规范,留着接下来模块使用.感觉还有很多能优化的地方,有时间看看源码,或者其他大神的代码 主要是Then 函数.回调有点绕人. !(function(win) { fu ...
- 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 ...
- Git学习(2)Git 安装
Windows 平台上安装 在 Windows 平台上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行: 安装包下载地址 ...
- wireshark使用教程
Wireshark: https://www.wireshark.org/ 安装: apt-get install wireshark 教程: http://blog.csdn.net/leichel ...
- 基础2 JVM
1. 内存模型以及分区,需要详细到每个区放什么. //运行时数据区域 方法区 Method Area 各个线程共享的内存区域 存储已被虚拟机加载的类信息 常量 静态变量 即时编译器编译后的代码 虚拟机 ...
- [js] 非常好的面试题,学习了。。
原文链接:http://www.cnblogs.com/xxcanghai/p/5189353.html
- 转:c的回归-云风
C 的回归 周末出差,去另一个城市给公司的一个项目解决点问题.回程去机场的路上,我用手机上 google reader 打发时间.第一眼就看到孟岩大大新的一篇:Linux之父话糙理不糙 .主题是 C ...
- python把str转换为int
def str2int(s): def fn(x,y): return x+y def char2num(s): ':9}[s] return reduce(fn,map(char2num,s)) ' ...
- python语法笔记(四)
1.对象的属性 python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统 对象的属性可能来自于其类定义,叫做类属性:还可能 ...