Android SearchBar 搜尋方塊介紹SearchManager

來源:互聯網
上載者:User

浮動搜尋方塊的使用其實並不難,而是在於它的配置非常之繁瑣,對於它的使用主要是方便開發人員對於程式中有搜尋業務時,更好的設計UI

SearchManager具體使用步驟如下:

(1)配置search bar的相關資訊,建立一個位於res/xml下的一個searchable.xml的設定檔,如預設值、是否有搜尋建議或者語音搜尋。

代碼

<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"
  <!-- 語音搜尋配置 -->
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="@string/search_invoke"  <!-- 配置搜尋建議,配置錯誤將不會顯示,這裡的searchSuggestAuthority的值必須是 繼承自SearchRecentSuggestionsProvider的完整路徑名 -->    android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"
android:searchSuggestSelection=" ? "
/>

(2) manifest.xml配置,搜尋結果處理的Activity將出現兩種情況,一種是從其他Activity中的search bar開啟一個Activtiy

專門處理搜尋結果,第二種是就在當前Activity就是處理結果的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>

 


<!-- 為了使每一個Activity都能使用search bar,一定要將這個標籤放到啟動Activity中,裡面的value指定
 的是前面的搜尋結果Activity-->
 <meta-data android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />

(3)搜尋建議在manifest.xml中相關的配置

<!--之前searchable.xml中有一個searchSuggestAuthority的值其實和這裡的authorities指向的都是name中所關聯的SearchSuggestionSampleProvider,他是一個SearchRecentSuggestionsProvider的子類--> <provider android:name="SearchSuggestionSampleProvider"  android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider>

 

上面authorities指向的都是name中所關聯的SearchSuggestionSampleProvider,他是一個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的方法,在介面上啟動一個search bar

但是這個動作不會自動觸發,必須通過一個按鈕或者菜單的點擊事件觸發;

代碼

    @Override
public boolean onSearchRequested(){

String text=etdata.getText().toString();
Bundle bundle=new Bundle();
bundle.putString("data", text);

//開啟浮動搜尋方塊(第一個參數預設添加到搜尋方塊的值)
//bundle為傳遞的資料
startSearch("mm", false, bundle, false);
//這個地方一定要返回真 如果只是super.onSearchRequested方法不但     //onSearchRequested(搜尋方塊預設值)無法添加到搜尋方塊中,bundle也無法傳遞出去
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())){
//擷取傳遞的資料
Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata=bundled.getString("data");
tvdata.setText(ttdata);

}else{
tvdata.setText("no data");
}
}
}

 之前說到了處理結果的Activity將可能出現的兩種情況的兩種,現在就處理第二種狀況,就是假如invoke search bar的

Activity同時也是處理搜尋結果的Activity,如果按照之前的方式處理則會出現一種情況,搜尋一次就執行個體化一次Activity,當按返回

鍵的時候會發現老是同一個Activity,其實為了使它只有一個執行個體化對象,只需簡單的配置和代碼就能實現

第一:在處理搜尋結果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())){
//擷取傳遞的資料
Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata=bundled.getString("data");
tvdata.setText(ttdata);

}else{
tvdata.setText("no data");
}
}
}

相關知識:上面講到了將最近搜尋值添加到搜尋建議中,但卻沒有提到如果清理搜尋建議中的值,與儲存相似,SearchRecentSuggestion對象提供了一個clearHistory()方法

 

代碼

    private void clearSearchHistory() {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}

忘了上:oye

 

from :http://www.cnblogs.com/jico/archive/2010/11/05/1869814.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.