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 使用(转)。的更多相关文章

  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. Maven仓库构建

    什么是Maven仓库 在不用Maven的时候,比如说以前我们用Ant构建项目,在项目目录下,往往会看到一个名为/lib的子目录,那里存放着各类第三方依赖jar文件,如log4j.jar,junit.j ...

  2. 触发器创建及Navicat中使用

    mysql中的触发器(trigger)使用 Trigger: 示例: mysql,)); Query OK, rows affected (0.03 sec) mysql> CREATE TRI ...

  3. 几种HtmlEncode的区别(转)

    一.C#中的编码 HttpUtility.HtmlDecode.HttpUtility.HtmlEncode与Server.HtmlDecode.Server.HtmlEncode与HttpServe ...

  4. eclipse xml 注释快捷键

    注释:CTRL + SHIFT + / 撤销注释:CTRL + SHIFT + \

  5. Android控件之CheckBox(复选框控件)

    一.有两种状态: 选中状态(true).未选中状态(false) 二.属性 android:id = "@+id/checkbox" android:layout_width=&q ...

  6. commonJS — 数组操作(for Array)

    for Array github: https://github.com/laixiangran/commonJS/blob/master/src/forArray.js 代码 /** * Creat ...

  7. bottomNavigationBar 底部导航tab MD

    1.先上图: 此底部Tab完全可以满足日常的开发 2.使用: 很简单,使用Gradle构建:compile ‘com.ashokvarma.android:bottom-navigation-bar: ...

  8. Selenium Grid Configuration

    Start Hub and Node with Json config 1. Start Hub with json config file title HubWebDriver java -jar ...

  9. Docker 使用指南 (二)—— 搭建本地仓库

    版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/94 来源:腾云阁 https://www.qclou ...

  10. 使用mybatis操作mysql数据库SUM方法返回NULL解决

    使用SQL语句用函数SUM叠加的时候,默认查询没有值的情况下返回的是NULL,而实际可能我们要用的是返回0 解决: SELECT SUM(total)   FROM test_table 改成: SE ...