浮動(dòng)搜索框的使用其實(shí)并不難,而是在于它的配置非常之繁瑣,對(duì)于它的使用主要是方便開發(fā)者對(duì)于程序中有搜索業(yè)務(wù)時(shí),更好的設(shè)計(jì)UI
SearchManager具體使用步驟如下:
(1)配置search bar的相關(guān)信息,新建一個(gè)位于res/xml下的一個(gè)searchable.xml的配置文件,如默認(rèn)值、是否有搜索建議或者語(yǔ)音搜索。
代碼
<searchable xmlns:android=http://schemas.android.com/apk/res/android <!-- label為搜索框上方的文本,hint搜索框里面的提示文本,顯示label -->
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchMode="showSearchLabelAsBadge"
<!-- 語(yǔ)音搜索配置 -->
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="@string/search_invoke" <!-- 配置搜索建議,配置錯(cuò)誤將不會(huì)顯示,這里的searchSuggestAuthority的值必須是 繼承自SearchRecentSuggestionsProvider的完整路徑名 --> android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"
android:searchSuggestSelection=" ? "
/>
(2) manifest.xml配置,搜索結(jié)果處理的Activity將出現(xiàn)兩種情況,一種是從其他Activity中的search bar打開一個(gè)Activtiy
專門處理搜索結(jié)果,第二種是就在當(dāng)前Activity就是處理結(jié)果的Activity,先介紹第一種配置:
代碼
<activity android:name="SearchResultActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH"></action>
</intent-filter> <!-- 指定上面的searchable.xml文件 --> <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>
</activity>
<!-- 為了使每一個(gè)Activity都能使用search bar,一定要將這個(gè)標(biāo)簽放到啟動(dòng)Activity中,里面的value指定
的是前面的搜索結(jié)果Activity-->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />
(3)搜索建議在manifest.xml中相關(guān)的配置
<!--之前searchable.xml中有一個(gè)searchSuggestAuthority的值其實(shí)和這里的authorities指向的都是name中所關(guān)聯(lián)的SearchSuggestionSampleProvider,他是一個(gè)SearchRecentSuggestionsProvider的子類--> <provider android:name="SearchSuggestionSampleProvider" android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider>
上面authorities指向的都是name中所關(guān)聯(lián)的SearchSuggestionSampleProvider,他是一個(gè)SearchRecentSuggestionsProvider的子類
代碼
public class SearchSuggestionSampleProvider extends SearchRecentSuggestionsProvider {
final static String AUTHORITY="com.android.cbin.SearchSuggestionSampleProvider"; final static int MODE=DATABASE_MODE_QUERIES; public SearchSuggestionSampleProvider(){ super(); setupSuggestions(AUTHORITY, MODE); }}
(4)為了能夠使用search bar 我們必須重寫Activity的onSearchRequested的方法,在界面上啟動(dòng)一個(gè)search bar
但是這個(gè)動(dòng)作不會(huì)自動(dòng)觸發(fā),必須通過一個(gè)按鈕或者菜單的點(diǎn)擊事件觸發(fā);
代碼
@Override
public boolean onSearchRequested(){
String text=etdata.getText().toString();
Bundle bundle=new Bundle();
bundle.putString("data", text);
//打開浮動(dòng)搜索框(第一個(gè)參數(shù)默認(rèn)添加到搜索框的值)
//bundle為傳遞的數(shù)據(jù)
startSearch("mm", false, bundle, false);
//這個(gè)地方一定要返回真 如果只是super.onSearchRequested方法不但 //onSearchRequested(搜索框默認(rèn)值)無(wú)法添加到搜索框中,bundle也無(wú)法傳遞出去
return true;
}
(5)接收query和bundle、保存query值(即搜索建議的列表值)
代碼
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())){
//獲取傳遞的數(shù)據(jù)
Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata=bundled.getString("data");
tvdata.setText(ttdata);
}else{
tvdata.setText("no data");
}
}
}
之前說(shuō)到了處理結(jié)果的Activity將可能出現(xiàn)的兩種情況的兩種,現(xiàn)在就處理第二種狀況,就是假如invoke search bar的
Activity同時(shí)也是處理搜索結(jié)果的Activity,如果按照之前的方式處理則會(huì)出現(xiàn)一種情況,搜索一次就實(shí)例化一次Activity,當(dāng)按返回
鍵的時(shí)候會(huì)發(fā)現(xiàn)老是同一個(gè)Activity,其實(shí)為了使它只有一個(gè)實(shí)例化對(duì)象,只需簡(jiǎn)單的配置和代碼就能實(shí)現(xiàn)
第一:在處理搜索結(jié)果Activity的manifest.xml中添加android:launchMode="singleTop"屬性
第二:重寫Activity的onNewIntent(Intent intent)
代碼
@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())){
//獲取傳遞的數(shù)據(jù)
Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata=bundled.getString("data");
tvdata.setText(ttdata);
}else{
tvdata.setText("no data");
}
}
}
相關(guān)知識(shí):上面講到了將最近的搜索值添加到搜索建議中,但卻沒有提到如果清理搜索建議中的值,與保存相似,SearchRecentSuggestion對(duì)象提供了一個(gè)clearHistory()方法
代碼
private void clearSearchHistory() {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}
聯(lián)系客服