如果你要在你的應用程序中實現(xiàn)搜索功能,android中為用戶提供兩種搜索的特性:
一種是search dialog,另一種是search widget.
由于search widget要在3.0以上的版本才能使用。這里只講search dialog
search dialog是由android系統(tǒng)控制的。需要由用戶去激活它。并且搜索框只出現(xiàn)在activity的最頂部。當提交查詢的數(shù)據(jù)時,系統(tǒng)會轉發(fā)給一個activity進行處理。用戶也可以保存最近查詢的數(shù)據(jù)。這里講一下基本的配置。
1.新建一個位于res/xml下的一個searchable.xml的配置文件
2.新建一個activity:SearchInvoke.java。此activity的作用是用來啟動search dialog并把要查詢的數(shù)據(jù)進行轉發(fā)給另一個activity進行處理。
關鍵代碼:
3.處理activity--》SearchQueryResults.java
3.創(chuàng)建類SearchSuggestionSampleProvider.java用戶查詢數(shù)據(jù)保存的配置信息
4.最重要的一步,在AndroidManifest.xml中的配置
5.無圖無真相
一種是search dialog,另一種是search widget.
由于search widget要在3.0以上的版本才能使用。這里只講search dialog
search dialog是由android系統(tǒng)控制的。需要由用戶去激活它。并且搜索框只出現(xiàn)在activity的最頂部。當提交查詢的數(shù)據(jù)時,系統(tǒng)會轉發(fā)給一個activity進行處理。用戶也可以保存最近查詢的數(shù)據(jù)。這里講一下基本的配置。
1.新建一個位于res/xml下的一個searchable.xml的配置文件
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
<!-- label為搜索框上方的文本,hint搜索框里面的提示文本 -->
android:label="@string/search_label"
android:hint="@string/search_hint"
android:icon="@drawable/search32"
android:searchMode="showSearchLabelAsBadge"
<!-- 中間是語音搜索配置(看不懂....) -->
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="Invoke Search"
<!-- 這里的值必須SearchSuggestionSampleProvider.AUTHORITY相同,后面講 -->
android:searchSuggestAuthority="SuggestionProvider"
android:searchSuggestSelection=" ? ">
</searchable>
2.新建一個activity:SearchInvoke.java。此activity的作用是用來啟動search dialog并把要查詢的數(shù)據(jù)進行轉發(fā)給另一個activity進行處理。
關鍵代碼:
public class SearchInvoke extends Activity{
private Button mStartSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_invoke);
//就一個按鈕
mStartSearch = (Button)findViewById(R.id.btn_start_search);
//啟動搜索框
mStartSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSearchRequested();
}
});
}
//重寫onSearchRequested方法
@Override
public boolean onSearchRequested() {
//除了輸入查詢的值,還可額外綁定一些數(shù)據(jù)
Bundle appSearchData = new Bundle();
appSearchData.putString("demo_key","text");
startSearch(null, false, appSearchData, false);
//必須返回true。否則綁定的數(shù)據(jù)作廢
return true;
}
}
3.處理activity--》SearchQueryResults.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_query_results);
//兩個TextView進行數(shù)據(jù)顯示
mQueryText = (TextView) findViewById(R.id.txt_query);
mAppDataText = (TextView) findViewById(R.id.txt_appdata);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onCreate()");
}
else {
mDeliveredByText.setText("onCreate(), but no ACTION_SEARCH intent");
}
}
/**
* Called when new intent is delivered.
這個方法不知道何時調用。看了文檔說在manifest中配置此activity的啟動模式為singleTop。不過試了貌似還沒有執(zhí)行到此方法
*/
@Override
public void onNewIntent(final Intent newIntent) {
super.onNewIntent(newIntent);
Log.i(TAG, "SearchQueryResults-->onNewIntent()");
// get and process search query here
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onNewIntent()");
}
else {
mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent");
}
}
//處理
private void doSearchQuery(final Intent queryIntent, final String entryPoint) {
//獲取查詢的值
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
mQueryText.setText(queryString);
//保存查詢的值,這樣在下次搜索的時候會有以前搜索數(shù)據(jù)的提示
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(queryString, null);
//獲得額外遞送過來的值
final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA);
if (appData == null) {
mAppDataText.setText("<no app data bundle>");
}
if (appData != null) {
String testStr = appData.getString("demo_key");
mAppDataText.setText((testStr == null) ? "<no app data>" : testStr);
}
}
3.創(chuàng)建類SearchSuggestionSampleProvider.java用戶查詢數(shù)據(jù)保存的配置信息
public class SearchSuggestionSampleProvider extends
SearchRecentSuggestionsProvider {
final static String AUTHORITY="SuggestionProvider";
final static int MODE=DATABASE_MODE_QUERIES;
public SearchSuggestionSampleProvider(){
super();
setupSuggestions(AUTHORITY, MODE);
}
}
4.最重要的一步,在AndroidManifest.xml中的配置
<!-- search ui -->
<activity android:name="com.zyz.app.SearchQueryResults"
android:label="處理查詢結果">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<!--此activity用來輸入并遞送結果,meta-data必須配置-->
<activity android:name="com.zyz.app.SearchInvoke">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value="com.zyz.app.SearchQueryResults"/>
</activity>
<!--provider的注冊-->
<provider android:name="com.zyz.app.SearchSuggestionSampleProvider"
android:authorities="SuggestionProvider" />
5.無圖無真相
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

