Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 搜索框架(Search Framework)實作

編輯:Android 開發實例

按下實體鍵盤上的搜尋按鈕時,會彈出一個搜尋對話框,並能顯示搜尋關鍵字的提示。這要如何實現呢?本文將一步步帶你完成。

一、配置搜尋描述檔

在 res/xml 中建立 searchable.xml,內容如下:

<searchable xmlns:android="https://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint"
    android:searchSettingsDescription="@string/settings_description">
</searchable>

二、建立 SearchableMusicActivity.java

至少需要實作 onCreate 方法把畫面顯示出來。

三、配置 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.halzhang.android.search" android:versionCode="1"
    android:versionName="1.0">
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity android:name=".SearchableMusicActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <!-- 配置 action -->
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <!-- 指定搜尋的設定檔 -->
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableMusicActivity" />
    </application>
</manifest>

四、實作搜尋提示提供者

Android 已經幫我們建立了一個預設的提示機制,我們只要繼承 SearchRecentSuggestionProvider 並稍作修改即可:

import android.content.SearchRecentSuggestionsProvider;

/**
 * 搜尋提示
 */
public class SearchSuggestionsProvider extends SearchRecentSuggestionsProvider {
    // 記住這個
    public final static String AUTHORITY = "searchprovider";
    public final static int MODE = DATABASE_MODE_QUERIES;

    public SearchSuggestionsProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}

五、完整配置 searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="https://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint"
    android:searchSettingsDescription="@string/search_settings_description"
    android:searchSuggestAuthority="searchprovider"
    android:searchSuggestIntentAction="android.intent.action.SEARCH"
    android:searchSuggestThreshold="1"
    android:includeInGlobalSearch="true"
    android:searchSuggestSelection=" ?" >
</searchable>

透過以上五個步驟,就能搭建出一個具備關鍵字提示功能的完整搜尋框架。其中 searchSuggestAuthority 對應前面建立的提示提供者,而 searchSuggestSelection 中的空格與問號則是查詢提示資料時的佔位參數,開發時要特別留意。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved