Android是google的產品,所以自然是少不了搜索。先看看Android一些應用中的搜索對話框。
圖1 Android中的全局搜索
圖2 聯繫人搜索
圖3 音樂搜索
以上都是通過按下實體鍵盤上的搜索按鈕彈出的一個搜索對話框,當然搜索關鍵詞提示是少不了的。如何實現呢?慢慢來!呵呵。
一、配置搜索描述文件
在res中的xml文件加創建sreachable.xml,內容如下:
-
<searchable xmlns:android="http://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="http://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" />
複製代碼
通過以上三步就能實現搜索對話框了。
接下來會講到搜索關鍵字提示,先到這裏。
-----------------EOF-------------------
1.創建搜索建議提供者
Android已經爲我們創建了一個默認的,我們只需要繼承 SearchRecentSuggestionProvider 就稍做修改就可以了。
見代碼:
-
import android.content.SearchRecentSuggestionsProvider;
-
-
/**
-
* 搜索提示
-
*
-
* @author Hanguo
-
* http://t.sina.com.cn/halzhang
-
* @version 2011-1-5上午11:51:39
-
*/
-
public class SearchSuggestionsProvider extends SearchRecentSuggestionsProvider {
-
//記住這個哦
-
public final static String AUTHORITY = "searchprovider";
-
-
public final static int MODE = DATABASE_MODE_QUERIES;
-
-
public SearchSuggestionsProvider() {
-
setupSuggestions(AUTHORITY, MODE);
-
}
-
}
複製代碼
2.配置searchable.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<searchable xmlns:android="http://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>
複製代碼
參數說明: android:searchSuggestAuthorith
此屬性的值就是SearchSuggestAuthorith中的AUTHORITH了。 android:searchSuggestIntentAction
此屬性定義了當我們選中搜索提示的內容時發生的目的動作。 android:searchSuggestThreshold
此屬性定義了至少輸入幾個字符時纔會彈出提示 android:includeInGlobalSearch
是否將內容加入android的全局搜索。true,加入。 android:searchSuggestSelection
定義搜索時參數的佔位符 |