十七、Android学习笔记_Android 使用 搜索框
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学习笔记_Android 使用 搜索框的更多相关文章
- 十四、Android学习笔记_Android回调函数触发的几种方式 广播 静态对象
一.通过广播方式: 1.比如登录.假如下面这个方法是外界调用的,那么怎样在LoginActivity里面执行登录操作,成功之后在回调listener接口呢?如果是平常的类,可以通过构造函数将监听类对象 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- 【转】 Pro Android学习笔记(七四):HTTP服务(8):使用后台线程AsyncTask
目录(?)[-] 5秒超时异常 AsyncTask 实现AsyncTask抽象类 对AsyncTask的调用 在哪里运行 其他重要method 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注 ...
- 【转】 Pro Android学习笔记(六七):HTTP服务(1):HTTP GET
目录(?)[-] HTTP GET小例子 简单小例子 出现异常NetworkOnMainThreadException 通过StrictMode进行处理 URL带键值对 Andriod应用可利用ser ...
- 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode
目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- 【转】Pro Android学习笔记(十三):用户界面和控制(1):UI开发
目录(?)[-] UI开发 方式一通过XML文件 方式二通过代码 方式三XML代码 UI开发 先理清一些UI概念: view.widget.control:这三个名词其实没有什么区别,都是一个UI元素 ...
- 【转】Pro Android学习笔记(十):了解Intent(上)
目录(?)[-] Intent基本含义 系统的Intent Android引入了Intent的概念来唤起components,component包括:1.Activity(UI元件) 2.Servic ...
- 【转】Pro Android学习笔记(二):开发环境:基础概念、连接真实设备、生命周期
在Android学习笔记(二):安装环境中已经有相应的内容.看看何为新.这是在source网站上的Android架构图,和标准图没有区别,只是这张图颜色好看多了,录之.本笔记主要讲述Android开发 ...
随机推荐
- WM_QUIT,WM_CLOSE,WM_DESTROY 消息出现顺序及调用方式
http://bbs.ednchina.com/BLOG_ARTICLE_3005455.HTM VC中WM_CLOSE.WM_DESTROY.WM_QUIT消息出现顺序及调用方式 wxleasyla ...
- 【机试题】c# 是否是素数,找出比它大的第一个素数
题目: 输入一个自然数 判断是否是素数,是素数则提示是素数,否则找出比它大的第一个素数 代码: Console.WriteLine("请输入任意一个自然数."); string n ...
- Codeforces Good Bye 2015 C. New Year and Domino 前缀和
C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...
- Microsoft Visual C++ Runtime Library Runtime Error的解决的方法
打开浏览器时,出现Microsoft Visual C++ Runtime Library Runtime Error错误,初步预计是软件冲突,可能有多种出错的方式,我的是浏览器自己主动关闭. 一. ...
- Linux makefile 教程 很具体,且易懂
近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了下面这篇文章.通俗易懂.然后把它贴出 ...
- Cocos2d-x v3.0 新的事件调度方法 lambda表达式的使用
欢迎添� Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/24603251 Coc ...
- 【Python】 做一个简单的 http server
# coding=utf-8 ''' Created on 2014年6月15日 @author: Yang ''' import socket import datetime # 初始化socket ...
- [原创]SSIS-执行包任务调用子包且子包读取父包变量
背景: 有时候需要将一个个开发好的独立的ETL包串接起来形成一个独立而庞大的包,如:每家分公司都开发不同的ETL包,最后使用执行包任务来将这些分公司的包给串联起来形成一个独立而完整运行的E ...
- 自己编写的.sh脚本文件运行完闪退解决方案
gnome-terminal设置如下图: 直接原因是,“命令退出时:退出终端”造成的!! 解决方案如下: 1. Ctrl + Alt + F1 ,进入文本操作模式: 2. 登录后,执行:yum ins ...
- windows 8.1 在硬盘上创建扩展分区
管理员-命令提示符-> diskpart -->进入分区工具. list disk -->显示电脑连接磁盘数. select disk * -->选择编号为*的 ...