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. ClouderManger搭建大数据集群时ERROR 2003 (HY000): Can't connect to MySQL server on 'ubuntucmbigdata1' (111)的问题解决(图文详解)

    问题详情 相关问题的场景,是在我下面的这篇博客里 Cloudera Manager安装之利用parcels方式(在线或离线)安装3或4节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(Ubun ...

  2. [linux] uptime 命令中关于平均负载的解释

    1.当前时间 00:13:25 2.系统已运行的时间 9小时19分 3.当前在线用户 2 user 4.平均负载:0.17, 0.12, 0.07 最近1分钟.5分钟.15分钟系统的负载 为了更好地理 ...

  3. HDU 2795——Billboard——————【单点更新、求最小位置】

    Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  4. iis win7 注册

    http://blog.sina.com.cn/s/blog_7ed5a8080100rinj.html vs2010默认的是4.0框架,4.0的框架是独立的CLR,和2.0的不同,如果想运行4.0的 ...

  5. Google android开发者 中国官方文档开放了呀

    Google官方开发文档地址 包括 android , android TV

  6. 工作流-----WorkFlow

    我们都知道对于一个OA系统来说,最重要的也是不可或缺的一个重要环节那就是对于工作流的实现,为此,最近专门在学习如何使用WorkFlow,问前辈,前辈也说道K2工作流引擎挺不错,自己同时也翻阅了一些资料 ...

  7. SpringMVC:系统认识一下maven

    网上看了很多springMVC helloworld的教程,非常不满意:首先,maven构建的就很少,再者,绝大部分都是断章取义,让人不明就里.其中有几篇讲的好的,我摘录了一些,自己试着构建了一下项目 ...

  8. Python 显示调用栈

    Python调试不如强类型的语言方便,显示调用栈有时非常必要,inspect模块很好用 import inspect inspect.stack() inspect.stack()返回的是一个函数栈帧 ...

  9. OLEDB 参数化查询

    一般情况下,SQL查询是相对固定的,一条语句变化的可能只是条件值,比如之前要求查询二年级学生信息,而后面需要查询三年级的信息,这样的查询一般查询的列不变,后面的条件只有值在变化,针对这种查询可以使用参 ...

  10. 通知:即日起本博客暂停更新,请移步至yanxin8.com获取最新文章

    通知:即日起本博客暂停更新,请移步至yanxin8.com与博主交流及获取最新文章