1、在资源文件夹下创建xml文件夹,并创建一个searchable.xml:

android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggestionsProvider类中的setupSuggestions方法的第一个参数相同。
android:searchSuggestSelection 指搜索参数
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.search.provider.MySuggestionProvider"
android:searchSuggestSelection=" ?">
</searchable>
2、配置文件 
  2.1 配置全局的搜索框
  启动的activity是SearchableActivity。分别在MainActivity和OtherActivity调用onSearchRequested()可以激活搜索框。映射是必须有"_id",
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.search"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.search.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- 放在外面就是全局 -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<!-- 点击搜索结果要跳转到的activity -->
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity android:name=".OtherActivity"></activity>
<provider
android:name="com.example.search.provider.MySuggestionProvider"
android:authorities="com.example.search.provider.MySuggestionProvider" />
</application> </manifest>

  2.2 为某一个Activity配置搜索框

为MainActivity配置了一个激活SearchableActivity的搜索框。

        <activity
android:name="com.example.search.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> <!-- 在某个activity的内部,表示当前的activity可以调出搜索框, 指定要激活的 SearchableActivity -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity> <!-- 点击搜索结果要跳转到的activity -->
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>

2.3 搜索之后,停留在当前Activity。

  如果停留在当前Activity,需要设置 launchMode="singleTop",并且在当前的Activity加入以下代码,还需要在onCreate方法里面调用handleIntent(intent)方法。

    @Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
} private void handleIntent(Intent intent) {if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
} private void doMySearch(String query) {
Toast.makeText(this, "res: "+query, Toast.LENGTH_SHORT).show();
}

  配置文件

      <activity
android:name="com.example.search.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<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>

3、创建provider

  需要继承SearchRecentSuggestionsProvider类,重写query方法,需要将查询出来的数据转化成MatrixCursor对象然后返回。为了进一步处理,需要将当前点击的项的参数通过SearchManager.SUGGEST_COLUMN_QUERY传过去,在activity接收时intent.getStringExtra(SearchManager.QUERY),在跳转的activity中,就可以继续进行操作。

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
// AUTHORITY:它的值域searchable.xml中的searchSuggestAuthority一样
public final static String AUTHORITY = "com.example.search.provider.MySuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES; public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
} @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// 在搜索框中输入的值
String query = selectionArgs[0]; Log.i("tag", query);
Log.i("tag", uri.getLastPathSegment().toLowerCase()); return getSuggestions(query);
} private Cursor getSuggestions(String query) {
String processedQuery = query == null ? "" : query.toLowerCase();
List<Person> persons = DataSource.getInstance().getPersons(processedQuery);
MatrixCursor cursor = new MatrixCursor(COLUMNS);
long id = 0;
for (Person person : persons) {
cursor.addRow(columnValuesOfWord(id++, person));
}
return cursor;
} private Object[] columnValuesOfWord(long id, Person person) {
return new Object[] { id, // _id
person.name, // text1
person.id, // text2
person.name };
} private static final String[] COLUMNS = { "_id",
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_QUERY
// SearchManager.SUGGEST_COLUMN_INTENT_DATA,// 数据传递到intenter中
}; }

http://www.cnblogs.com/zhengbeibei/archive/2013/01/17/2865610.html

Android学习笔记_79_ Android 使用 搜索框的更多相关文章

  1. Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...

    下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...

  2. Android学习笔记之Android Studio添加新的Activity

    1.创建Android项目工程:AndroidTest 创建过程可参考网上诸多教程. 2.添加新的Activity,步骤如下 a. 在layout文件夹上右键,New-Activity-相应Activ ...

  3. Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns

    摘自:http://blog.csdn.net/sjf0115/article/details/7213565/ TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但 ...

  4. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  5. Android学习笔记之Android Studio下创建menu布局文件

    1.创建menu文件夹 Android Studio项目中如果没有menu文件夹,在res文件夹右键,new-Android resource directory: 则会弹出下图对话框,在Resour ...

  6. android学习笔记(9)android程序调试学习

    相应若水老师的第十四课 一,Log日志输出 Log.v(tag,message);        //verbose模式,打印最具体的日志  Log.d(tag,message);        // ...

  7. Android学习笔记(1)—Android Studio安装

    Android Studio 是一个全新的 Android 开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工 ...

  8. Android学习笔记1——Android开发环境配置

    一.JDK配置 Android是基于Java进行开发的,首先需要在电脑上配置JDK(Java Development Kit).在http://www.androiddevtools.cn/下载对应系 ...

  9. Android学习笔记(36):Android的两种事件处理方式

    Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...

随机推荐

  1. linux下安装redis及PHP扩展应用

    一.redis安装 1 下载redis安装包 wget http://redis.googlecode.com/files/redis-2.4.17.tar.gz (若无法下载请手动下载) 2 编译安 ...

  2. Basic Data Structures and Algorithms in the Linux Kernel--reference

    http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...

  3. Windows的加密能力

    尽管Windows不再具备往日那样的统治地位,在智能手机领域,甚至已经沦落为一种小众平台,Windows仍然是主要的商业应用运行平台.软件开发平台.硬件及结构等设计软件运行平台.大多数人在学习计算机时 ...

  4. 03.if 和 switch结合练习

    namespace _04.练习01 { class Program { static void Main(string[] args) { //请用户输入年份,再输入月份,输出该月有多少天 Cons ...

  5. main方法击破

    什么是main方法? 是类中的一段代码,可以让程序独立运行. public class HelloWord{ public static void main(String[] args) { for ...

  6. Visual Paradigm for UML 10.0 SP1 企业中文下载地址、安装及激活详解教程

    https://blog.csdn.net/u013354805/article/details/46531833

  7. centOs升级

    因为军佬放弃制作Centos7的网络重装包,又Centos7的安装引导和6有较大区别所以,选择曲线救国(技术不行,只能这样乱搞)前文:Centos6.9一键重装包https://ppx.ink/net ...

  8. 学习 JavaScript 树

    学习 JavaScript 树 树(Tree) 在计算机科学中,数据结构是一门研究非数值计算的程序设计问题中计算机的操作对象(数据元素)以及它们之间的操作和运算等的学科. 它包含三方面的内容: 数据的 ...

  9. 刚在虚拟机上装的Linux系统,ifconfig后IP地址怎么成了127.0.0.1了

    之前在虚拟机上装了Linux系统,用了一段时间后想删除了重新装一下,然而装完以后ifconfig后,出现的是 [root@localhost ~]# ifconfig lo Link encap:Lo ...

  10. matlab练习程序(Ritter‘s最小包围圆)

    原始算法是sphere,我这里简化为circle了. Ritter's求最小包围圆为线性算法,因为非常简单,所以应用非常广泛. 该算法求出的圆比最优圆大概会大个5%到20%左右,求最优圆应该可以用Bo ...