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