先上:
步驟:
(1)配置search bar的相關資訊,建立一個位於res/xml下的一個searchable.xml的設定檔
<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/searchHint" android:searchMode="showSearchLabelAsBadge" android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider" android:searchSuggestSelection=" ? "> </searchable>
(2) manifest.xml配置,搜尋結果處理的Activity將出現兩種情況,一種是從其他Activity中的search bar開啟一個Activtiy
專門處理搜尋結果,第二種是就在當前Activity就是處理結果的Activity,這配置裡包含兩種情況,自己可以看代碼能分辨出來。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.cbin" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name"> <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=".SearchResultActivity" /> </activity> <activity android:name="SearchResultActivity" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.SEARCH"></action> </intent-filter> <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data> </activity><provider android:name="SearchSuggestionSampleProvider" android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider></application> <uses-sdk android:minSdkVersion="7" /></manifest>
(3 ) 儲存記錄 上面authorities指向的都是name中所關聯的SearchSuggestionSampleProvider,他是一個SearchRecentSuggestionsProvider的子類
package com.android.search;import android.content.SearchRecentSuggestionsProvider;public class SearchSuggestionSampleProvider extends SearchRecentSuggestionsProvider { final static String AUTHORITY="com.android.search.SearchSuggestionSampleProvider"; final static int MODE=DATABASE_MODE_QUERIES; public SearchSuggestionSampleProvider(){ super(); setupSuggestions(AUTHORITY, MODE); }}
(4)為了能夠使用search bar 我們必須重寫Activity的onSearchRequested的方法,在介面上啟動一個search bar
但是這個動作不會自動觸發,必須通過一個按鈕或者菜單的點擊事件觸發;
package com.android.search;import com.android.search.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Main extends Activity implements OnClickListener{ /** Called when the activity is first created. */ private EditText etdata; private Button btnsearch; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findview(); } private void findview(){ etdata=(EditText)findViewById(R.id.etdata); btnsearch=(Button)findViewById(R.id.btncall); btnsearch.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub onSearchRequested(); } @Override public boolean onSearchRequested(){ String text=etdata.getText().toString(); Bundle bundle=new Bundle(); bundle.putString("data", text); //開啟浮動搜尋方塊(第一個參數預設添加到搜尋方塊的值) //bundle為傳遞的資料 startSearch("哈哈", false, bundle, false); //這個地方一定要返回真 如果只是super.onSearchRequested方法 //不但onSearchRequested(搜尋方塊預設值)無法添加到搜尋方塊中 //bundle也無法傳遞出去 return true; } }
(5) 在本Activity中搜尋
package com.android.search;import com.android.search.R;import android.app.Activity;import android.app.SearchManager;import android.content.Intent;import android.os.Bundle;import android.provider.SearchRecentSuggestions;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class SearchResultActivity extends Activity implements OnClickListener{ private TextView tvquery,tvdata; private Button btnsearch; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.searchresult); tvquery=(TextView)findViewById(R.id.tvquery); tvdata=(TextView)findViewById(R.id.tvdata); btnsearch=(Button)findViewById(R.id.btnSearch); doSearchQuery(); btnsearch.setOnClickListener(this); } public void doSearchQuery(){ final Intent intent = getIntent(); //獲得搜尋方塊裡值 String query=intent.getStringExtra(SearchManager.QUERY); tvquery.setText(query); //儲存搜尋記錄 SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this, SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE); suggestions.saveRecentQuery(query, null); if(Intent.ACTION_SEARCH.equals(intent.getAction())){ //擷取傳遞的資料 Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA); if(bundled!=null){ String ttdata=bundled.getString("data"); tvdata.setText(ttdata); }else{ tvdata.setText("no data"); } } } @Override public void onClick(View v) { // TODO Auto-generated method stub onSearchRequested(); } @Override public boolean onSearchRequested(){ startSearch("onNewIntent", false, null, false); return true; } @Override public void onNewIntent(Intent intent){ super.onNewIntent(intent); //獲得搜尋方塊裡值 String query=intent.getStringExtra(SearchManager.QUERY); tvquery.setText(query); //儲存搜尋記錄 SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this, SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE); suggestions.saveRecentQuery(query, null); if(Intent.ACTION_SEARCH.equals(intent.getAction())){ //擷取傳遞的資料 Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA); if(bundled!=null){ String ttdata=bundled.getString("data"); tvdata.setText(ttdata); }else{ tvdata.setText("no data"); } } }}