閱讀172 返回首頁    go iPhone_iPad_Mac_apple


Android---浮動搜索框(SearchManager)

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

SearchManager具體使用步驟如下:

(1)配置search bar的相關信息,新建一個位於res/xml下的一個searchable.xml的配置文件,如默認值、是否有搜索建議或者語音搜索。

複製代碼
代碼


   <!-- 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(); }
複製代碼

接下來的介紹更加詳細一些:

當您需要在您的應用程序中提供搜索服務時,您第一個想到的是您的搜索框要放哪呢?通過使用Android的搜索框架,應用程序將顯示一個自定義搜索對話框來處理用戶的搜索請求。通過一個簡單的搜索按鈕或從您的應用程序中調用API,搜索對話框就會顯示在屏幕的頂部,並會自動顯示您的應用程序圖標。如下圖所示:

如何使用Android的搜索框架_28091

本文將教你如何為你的應用程序提供一個自定義搜索對話框。這樣做,給您的用戶提供一個標準化的搜索體驗,並能增加如語音搜索和搜索建議等功能。

基礎知識

Android的搜索框架將代您管理的搜索對話框,您不需要自己去開發一個搜索框,不需要擔心要把搜索框放什麼位置,也不需要擔心搜索框影響您當前的界麵。所有的這些工作都由SearchManager類來為您處理(以下簡稱“搜索管理器”),它管理的Android搜索對話框的整個生命周期,並執行您的應用程序將發送的搜索請求,返回相應的搜索關鍵字。

當用戶執行一個搜索,搜索管理器將使用一個專門的Intent把搜索查詢的關鍵字傳給您在配置文件中配置的處理搜索結果的Activity。從本質上講,所有你需要的就是一個Activity來接收Intent,然後執行搜索,並給出結果。具體來說,你需要的做的事就包括以下內容:

一個搜索配置
我們用個XML配置文件來對搜索對話框進行配置,包括一些功能的配置,如文本框,設置語音搜索和搜索建議中顯示的提示文字等。

一個用來處理搜索請求的Activity
這個Activity用來接收搜索查詢的內容,然後搜索您的數據並顯示搜索結果。

一種用戶執行搜索的途徑
默認情況下,一旦你配置了一個可搜索的Activity,設備搜索鍵(如果有)將調用搜索對話框。然而,你應該始終提供另一種手段,讓用戶可以調用搜索對話框,如在選項菜單中的搜索按鈕或其他用戶界麵上的按鈕,因為不是所有的設備提供一個專門的搜索鍵。


創建一個搜索對話框配置文件

搜索框配置文件是一個用來配置您的應用程序中搜索框的設置的XML文件,這個文件一般命名為searchable.xml,並且必須保存在項目的res/xml/目錄下。

配置文件的根節點必須為,可以有一個或多個屬性。如下所示:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:andro
  3.     android:label="@string/searchLabel" android:hint="@string/searchHint">
  4. </searchable>
複製代碼
上麵的配置文件中,除android:hint屬性外,其它都是一個搜索對話框必須的配置項,android:label是一個必須的屬性,它的值為一個string資源引用,不能直接用字符串,通常會是應用程序的名稱(盡管它是一個必須的屬性,但通常情況下是不顯示出來的,除非你開啟了搜索建議功能)。android:hint是配置搜索框的輸入提示信息,也必須引用string.xml中配置的字符串資源,不能直接使用字符串。

可以配置很多的屬性,但大部分屬性都隻是在使用搜索建議和語音搜索時進行配置,盡管如此,我們建議你一定要配置android:hint,用於提示用戶需要輸入的信息。

接下來,你需要把這個配置文件放到你的應用程序中。

創建一個可用於搜索的Activity

當用戶從一個搜索框執行搜索時,搜索管理器(Search Manager)會通過ACTION_SEARCH Intent 把要搜索的內容(關鍵字)發送到一個可執行搜索的Activity。這個Acitivity查詢數據並顯示結果。

定義一個可搜索的Activity

如果你還沒有準備好,那麼就創建一個用來執行搜索的Activity,聲明它可以響應ACTION_SEARCH Intent ,並且增加搜索框配置信息。為此,你需要添加一個元素和一個元素在你的manifest文件中的節點。如下所示:
  1. <application ... >
  2.     <activity android:name=".MySearchableActivity" >
  3.         <intent-filter>
  4.             <action android:name="android.intent.action.SEARCH" />
  5.         </intent-filter>
  6.         <meta-data android:name="android.app.searchable"
  7.                   android:resource="@xml/searchable"/>
  8.     </activity>
  9.     ...
  10. </application>
複製代碼
中的android:name屬性值必須為”android.app.searchable”,android:resource屬性值必須引用上麵提到的res/xml/目錄下的搜索配置文件(本例中的res/xml/searchable.xml)。

請注意,隻有配置了上麵的meta-data節點的Activity的節點才能執行搜索,如果想在整個應用程序中都可以調用搜索框,可以進行如下配置:
  1. <application ... >
  2.     <activity android:name=".MySearchableActivity" >
  3.         <intent-filter>
  4.             <action android:name="android.intent.action.SEARCH" />
  5.         </intent-filter>
  6.         <meta-data android:name="android.app.searchable"
  7.                   android:resource="@xml/searchable"/>
  8.     </activity>
  9.     <activity android:name=".AnotherActivity" ... >
  10.     </activity>
  11.     <!—這個配置就可以讓你在整個應用程序中調用搜索框 -->
  12.     <meta-data android:name="android.app.default_searchable"
  13.               android:value=".MySearchableActivity" />
  14.     ...
  15. </application>
複製代碼
上麵代碼中android:name=”android.app.default_searchable” 定義一個響應搜索框搜索請求的名稱,android:value指定是由哪個Activity響應並執行搜索。當我們在應用程序中的 OtherAcitivity中執行搜索請求時,MySearchableActivity將會被加載用於執行搜索並顯示搜索結果。


執行一個搜索

當一個Activity聲明為可搜索時,執行實際的搜索包括三個步驟:接收查詢,檢索你的數據,並提交結果。

通常情況下,你的搜索結果需要在一個ListView中展現,所以你用於執行搜索的Acitivity要繼承ListActivity,這樣,可以方便的訪問ListView的Api。

接收搜索查詢

當從搜索對話框執行搜索時,剛才配置的可用於搜索的Acitivity將會被Intent激活,同時帶著一些搜索相關的參數,你需要檢查Intent並做出搜索響應,如下所示:

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(R.layout.search);

  5.     Intent intent = getIntent();
  6. //判斷是否是搜索請求
  7.     if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  8. //獲取搜索的查詢內容(關鍵字)
  9.       String query = intent.getStringExtra(SearchManager.QUERY);
  10. //執行相應的查詢動作
  11.       doMySearch(query);
  12.     }
  13. }
複製代碼
doMySearch()方法將根據關鍵字查詢數據庫,或從網絡上查詢數據,如果是耗時的搜索,你還需要使用進度條,來告訴用戶搜索正在進行,最後返回結果後,可以調用ListView的setAdapter()方法將結果顯示在ListView中。

調用搜索對話框

你可以從應用程序中的任何一個地方調用onSearchRequested()方法激活搜索框,比如從菜單中或者一個按鈕等。你也要以在 onCreate()方法中調用setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL),這樣,當用戶按下鍵盤上的按鍵時,將會自動激活搜索框。

搜索框和普通對話框一樣,浮動在屏幕的最上方,它不會改變任何Activity堆棧狀態,沒有任何Activity生命周期中的方法會被調用,隻是當搜索框出現就,正在運行的Activity會失去輸入焦點。

如果你要在執行搜索時,進行別的操作,可以重寫onSearchRequested()方法,如下所示:
  1. @Override
  2. public boolean onSearchRequested() {
  3. //這個方法中幹你想幹的事,比如做一些被始化工作
  4.     pauseSomeStuff();
  5.     return super.onSearchRequested();
  6. }
複製代碼
如果當前的Activity就是響應搜索請求的Activity時,會有以下兩種情況:

默認情況下,ACTION_SEARCH Intent將會創建一個新的Activity,並調用onCreate()方法,這個新的Activity會顯示在最前麵,你將同時有兩個 Activity實例。當你按“返回”鍵裏,會回到沒有執行搜索前的一個Activity。

另一種情況是配置了android:launchMode=”singleTop”的Activity,這時,我們需要在 onNewIntent(Intent)方法中處理搜索請求,如下所示:
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(R.layout.search);
  5.     handleIntent(getIntent());
  6. }

  7. @Override
  8. protected void onNewIntent(Intent intent) {
  9.     setIntent(intent);
  10.     handleIntent(intent);
  11. }

  12. private void handleIntent(Intent intent) {
  13.     if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  14.       String query = intent.getStringExtra(SearchManager.QUERY);
  15.       doMySearch(query);
  16.     }
  17. }


相應的Activity配置如下

  1. <activity android:name=".MySearchableActivity"
  2.               android:launchMode="singleTop" >
  3.     <intent-filter>
  4.         <action android:name="android.intent.action.SEARCH" />
  5.     </intent-filter>
  6.     <meta-data android:name="android.app.searchable"
  7.                       android:resource="@xml/searchable"/>
  8.   </activity>
複製代碼
如何給搜索框增加參數

要給搜索框傳遞參數,我們需要重寫onSearchRequested()方法,如下所示:
  1. @Override
  2. public boolean onSearchRequested() {
  3.     Bundle appData = new Bundle();
  4.     appData.putBoolean(MySearchableActivity.JARGON, true);
  5.     startSearch(null, false, appData, false);
  6.     return true;
  7. }
複製代碼
我們的Activity在收到搜索框的搜索請求時,通過如下方法獲取參數:
  1. Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
  2. if (appData != null) {
  3.     boolean jargon = appData.getBoolean(MySearchableActivity.JARGON);
  4. }
複製代碼
最後我們來看看如何使用android的語音搜索:

隻需要對我們的搜索配置文件做如下改動,你的搜索就支持語音搜索了,配置文件如下所示:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:andro
  3.     android:label="@string/searchLabel"
  4.     android:hint="@string/searchHint"
  5.     android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
  6. </searchable>
複製代碼
好了,今天就到這兒,自己動手練習去吧!以後還會詳細講解如何增加搜索建議,語音搜索等內容。

關於搜索建議的智能提示

可參考:Adding Custom Suggestions
_28093





最後更新:2017-04-03 12:55:25

  上一篇:go Oracle 查詢表中各列名稱、表中列數
  下一篇:go vs2010 修改編譯生成dll存放路徑