Android 浮动搜索框的使用
首先展示一下我demo示例的项目结构,如下图:?其中?
- MainActivity.java 是应用程序的第一个Activity
- SearchResultActivity.java 是搜索结果Activity
- SearchSuggestionSampleProvider.java 是搜索建议提供类
- searchable.xml是搜索框的布局文件
1)首先在res目录下建立xml/searchable.xml布局文件<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/search_label"android:hint="@string/search_hint"android:searchMode="showSearchLabelAsBadge"<!-- 搜索建议提供类-->android:searchSuggestAuthority="org.freedom.search.SearchSuggestionSampleProvider"android:searchSuggestSelection=" ? "></searchable>?
2)strings.xml<?xml version="1.0" encoding="utf-8"?><resources>? ? <string name="hello">搜索浮动框</string>? ? <string name="app_name">搜索浮动框示例</string>? ? <string name="search_label">搜索</string>? ? <string name="search_hint">请输入搜索关键字</string></resources>
3) 应用主界面 MainActivity.javapackage org.freedom.search;
import org.freedom.search.R;import android.app.Activity;import android.os.Bundle;
public class MainActivity extends Activity {? ??? ? @Override? ? protected void onCreate(Bundle savedInstanceState) {? ?? super.onCreate(savedInstanceState);? ?? setContentView(R.layout.main);? ? }}
4)搜索结果界面 SearchResultActivity.javapackage org.freedom.search;
import android.app.Activity;import android.app.SearchManager;import android.content.Intent;import android.os.Bundle;import android.provider.SearchRecentSuggestions;import android.widget.TextView;
public class SearchResultActivity extends Activity {private TextView result = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);result = new TextView(this);setContentView(result);setTitle("搜索结果");Intent intent = getIntent();if(Intent.ACTION_SEARCH.equals(intent.getAction())){String queryKeyword = intent.getStringExtra(SearchManager.QUERY);saveRecentSearchHistory(queryKeyword);doSearch(queryKeyword);}}
//保存最近搜索的关键字记录private void saveRecentSearchHistory(String queryKeyword) {SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);suggestions.saveRecentQuery(queryKeyword, null);}
//清除最近搜索的关键字记录private void clearRecentSearchHistory() {SearchRecentSuggestions suggestions =new SearchRecentSuggestions(this,?SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);suggestions.clearHistory();}private void doSearch(String queryKeyword) {result.setText("Your search keyword is : " queryKeyword);}@Overrideprotected void onDestroy() {clearRecentSearchHistory();super.onDestroy();}
}
5)搜索建议提供类?SearchSuggestionSampleProvider.javapackage org.freedom.search;
import android.content.SearchRecentSuggestionsProvider;
public class SearchSuggestionSampleProvider extends SearchRecentSuggestionsProvider{public final static String AUTHORITY = SearchSuggestionSampleProvider.class.getName();public final static int MODE = DATABASE_MODE_QUERIES;
public SearchSuggestionSampleProvider() {super();setupSuggestions(AUTHORITY, MODE);}}
6)AndroidMainfest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"? ? ? package="org.freedom.search"? ? ? android:versionCode="1"? ? ? android:versionName="1.0">
? ? <application android:icon="@drawable/icon" android:label="@string/app_name">? ? ? ? <activity android:name=".MainActivity"? ? ? ? ? ? ? ? ? android:label="@string/app_name">? ? ? ? ? ? <intent-filter>? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />? ? ? ? ? ? </intent-filter>? ? ? ? </activity>? ? ? ? <activity android:name=".SearchResultActivity">? ? ? ?? <intent-filter>? ? ? ?? <action android:name="android.intent.action.SEARCH" />? ? ? ?? </intent-filter>? ? ? ?? <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>? ? ? ? </activity>? ? ? ??? ? ? ? <!-- 这个配置就可以让你在整个应用程序中调用搜索框 -->? ?? <meta-data android:name="android.app.default_searchable" android:value=".SearchResultActivity" />
<!-- 配置搜索建议提供方 --><provider android:name="SearchSuggestionSampleProvider"android:authorities="org.freedom.search.SearchSuggestionSampleProvider"></provider>? ? </application></manifest>
运行效果如下图:?????注意:如果你的应用是直接在当前界面搜索,并在当前界面显示结果(也就是所在SearchResultActivity.java搜索,并在该界面显示)。那么上面的代码需要做如下调整。
调整后的SearchResultActivity.java文件如下:package org.freedom.search;
import android.app.Activity;import android.app.SearchManager;import android.content.Intent;import android.os.Bundle;import android.provider.SearchRecentSuggestions;import android.widget.TextView;
public class SearchResultActivity extends Activity {private TextView result = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);result = new TextView(this);setContentView(result);setTitle("搜索结果");doSearch(getIntent());}@Overrideprotected void onNewIntent(Intent intent) {setIntent(intent);doSearch(intent);}
private void saveRecentSearchHistory(String queryKeyword) {SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);suggestions.saveRecentQuery(queryKeyword, null);}
private void clearRecentSearchHistory() {SearchRecentSuggestions suggestions =new SearchRecentSuggestions(this,?SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);suggestions.clearHistory();}private void doSearch(Intent intent) {if(Intent.ACTION_SEARCH.equals(intent.getAction())){String queryKeyword = intent.getStringExtra(SearchManager.QUERY);saveRecentSearchHistory(queryKeyword);result.setText("Your search keyword is : " queryKeyword);}}@Overrideprotected void onDestroy() {clearRecentSearchHistory();super.onDestroy();}
}
同时需要修改AndroidManifest,xml文件(-号代表删除, 号代表添加)-<activity android:name=".SearchResultActivity"><activity android:name=".SearchResultActivity" android:launchMode="singleTop">