編輯:Android 開發實例
在 Map 應用中經常會看到一個浮動的搜尋框,多半用來搜尋附近的 POI 點,而且這些功能長得都大同小異。查了一下才發現,SDK 的 Dev Guide 早有詳細介紹,只是全英文,讀起來頗費工夫。功能本身並不難,麻煩的是配置瑣碎,以下逐步說明。
這是一個用來設定應用程式搜尋框的 XML 檔,通常命名為 searchable.xml,並放在專案的 res/xml/ 目錄下。根節點必須是 searchable。在 SearchableInfo 中,Android 透過 com.android.internal.R.styleable.Searchable 這個 ID 讀取配置,因此標籤名稱必須固定為 searchable;至於檔名則無關緊要,重點是一定要放進 res/xml/ 目錄。內容大致如下:
- <?xml version="1.0" encoding="utf-8"?>
- <searchable xmlns:android="https://schemas.android.com/apk/res/android"
- android:label="@string/searchLabel" android:hint="@string/searchHint"
- android:icon="@drawable/menu_route" android:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider"
- android:queryAfterZeroResults="false" android:searchSuggestSelection=" ? ">
- </searchable>
其中 android:icon 原以為能設定搜尋文字前方那個 View 的圖示,實測卻不起作用,文件中也未提及,看來確實無效,這屬性讓我折騰了好久,留待後續再談。另外要注意,android:label 與 android:hint 不能直接寫字面值,而必須指向 values 中定義的字串資源;其中 android:hint 就是輸入框為空時顯示的提示文字。
此外還有大量與語音搜尋相關的配置,這個功能相對冷門,有興趣者可自行查閱文件。完整的屬性清單如下:
- <?xml version="1.0" encoding="utf-8"?>
- <searchable xmlns:android="https://schemas.android.com/apk/res/android"
- android:label="string resource"
- android:hint="string resource"
- android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
- android:searchButtonText="string resource"
- android:inputType="inputType"
- android:imeOptions="imeOptions"
- android:searchSuggestAuthority="string"
- android:searchSuggestPath="string"
- android:searchSuggestSelection="string"
- android:searchSuggestIntentAction="string"
- android:searchSuggestIntentData="string"
- android:searchSuggestThreshold="int"
- android:includeInGlobalSearch=["true" | "false"]
- android:searchSettingsDescription="string resource"
- android:queryAfterZeroResults=["true" | "false"]
- android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
- android:voiceLanguageModel=["free-form" | "web_search"]
- android:voicePromptText="string resource"
- android:voiceLanguage="string"
- android:voiceMaxResults="int"
- >
- <actionkey
- android:keycode="KEYCODE"
- android:queryActionMsg="string"
- android:suggestActionMsg="string"
- android:suggestActionMsgColumn="string" >
- </searchable>
這個 Activity 可以新建,也可以直接用彈出搜尋框的當前 Activity。本文的 MapActivity 就是顯示結果的地方,因此直接沿用。兩種做法配置相近,差別在於結果接收的方式。首先在 Activity 標籤中加入 intent-filter 與 meta-data:
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <meta-data android:name="android.app.searchable"
- android:resource="@xml/searchable"/>
這樣只在該 Activity 中啟用搜尋。若希望整個 Application 都能使用搜尋,則需建立一個專門處理搜尋的 Activity,並在 application 標籤下宣告預設 searchable:
- <!-- declare the default searchable Activity for the whole app -->
- <meta-data android:name="android.app.default_searchable"
- android:value=".MySearchableActivity" />
所有 Activity 都能呼叫 onSearchRequested() 讓搜尋框出現。測試時有個小技巧:在 onCreate() 中呼叫 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL),使用者按下實體鍵盤按鍵時就會自動啟動搜尋框。若想觸發時順便做別的事,可覆寫 onSearchRequested():
- @Override
- public boolean onSearchRequested() {
- //這個方法中干你想干的事
- doSometingOther();
- return super.onSearchRequested();
- }
呼叫時若想傳遞參數,可以改用 startSearch():
- public boolean onSearchRequested() {
- Log.i(TAG,"onSearchRequested------------========");
- Bundle appData = new Bundle();
- appData.putString("key", "your info");
- startSearch(null, true, appData, false);
- return true;
- }
若是專門處理查詢的 Activity,可直接在 onCreate 中執行:
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.search);
- Intent intent = getIntent();
- if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- doMySearch(query);
- }
- }
但若是沿用當前 Activity,onCreate 只會執行一次,此時就必須靠 onNewIntent 來接收:
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.search);
- handleIntent(getIntent());
- }
- @Override
- protected void onNewIntent(Intent intent) {
- setIntent(intent);
- handleIntent(intent);
- }
- private void handleIntent(Intent intent) {
- if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- doMySearch(query);
- }
- }
如此便會透過 doMySearch() 完成查詢。不過要注意:查詢完按返回時,你會發現 Activity 堆疊裡有兩個 MapActivity 實例,這可以透過 android:launchMode="singleTop" 解決:
- <activity android:name=".GoogleMapActivity" android:launchMode="singleTop"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <meta-data android:name="android.app.searchable"
- android:resource="@xml/searchable"/>
- </activity>
查詢完成後,往往希望能保存這次的查詢條件。Android 透過 SearchRecentSuggestionsProvider 實作了保存關鍵字的功能。首先建立一個 Provider 類:
- public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
- /**
- * Authority
- */
- final static String AUTHORITY = "com.debby.googlemap.SuggestionProvider";
- /**
- * Mode
- */
- final static int MODE = DATABASE_MODE_QUERIES;
- public SearchSuggestionProvider() {
- super();
- setupSuggestions(AUTHORITY, MODE);
- }
- }
接著在 Manifest 中註冊,注意 android:authorities 要與 Provider 內保持一致:
- <provider android:name="com.debby.googlemap.SearchSuggestionProvider"
- android:authorities="com.debby.googlemap.SuggestionProvider" />
如此一來,就能在 Activity 中呼叫保存:
- SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
- SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
- suggestions.saveRecentQuery(query, null);
這樣關鍵字就保存成功,下次搜尋時即可看到歷史紀錄。有些時候我們還想保存查詢結果,例如在地圖上查過某個地點,希望下次能快速重現——這時可以把該地點的經緯度等資訊存起來,直接在 handleIntent() 中透過 SQLite 手動操作資料庫即可,屬於一般資料庫操作,這裡不再贅述。
Android MediaPlayer(多媒體播放):音訊與影片控制入門
Android 提供了許多方法來控制播放的音訊/影片檔案與串流,其中主要的一種是透過名為 MediaPlayer 的類別。 MediaPlayer 類別讓 Android
Android 開發之時間日期操作實例
對於手機的時間日期設置,大家應該都不陌生。今天就用一個時間日期設置的示例來做說明,其中尚有可完善之處,例如如何讓設置的時間日期與手機系統同步等,有興趣的讀者可以結合自身經
深入淺析 Android Fragment(上篇):從誕生到動態操作
曾有一段時間,好像聊什麼都能扯到 Fragment,做什麼都要問一句「Fragment 能不能實現」——現在回想起來確實有點誇張,但它會紅,背後是有明確理由的。 Fra
Android 登入畫面實作:帳密驗證與失敗鎖定
登入畫面幾乎是每個需要帳號的 App 都會遇到的介面。使用者在此輸入憑證,App 驗證通過後才放行到下一個頁面。本章要拆解一個登入畫面的三個組成部分:輸入欄位、驗證邏輯,