Android开发——Android搜索框架

来源:互联网 发布:同志网络剧国产2017 编辑:程序博客网 时间:2024/05/16 08:04

Android是google的产品,所以自然是少不了搜索。先看看Android一些应用中的搜索对话框。

图1 Android中的全局搜索

图2 联系人搜索

图3 音乐搜索

以上都是通过按下实体键盘上的搜索按钮弹出的一个搜索对话框,当然搜索关键词提示是少不了的。如何实现呢?慢慢来!呵呵。

一、配置搜索描述文件

在res中的xml文件加创建sreachable.xml,内容如下:

   
  1. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:label="@string/search_label"
  3. android:hint="@string/search_hint"
  4. android:searchSettingsDescription="@string/settings_description">
  5. </searchable>
复制代码

二、创建SearchableMusicActivity.java

至少需要实现onCreate方法显示出来吧。

三、配置AndroidManifest.xml

  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.halzhang.android.search" android:versionCode="1"
  4. android:versionName="1.0">
  5. <application android:icon="@drawable/icon" android:label="@string/app_name">
  6. <activity android:name=".SearchableMusicActivity"
  7. android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN" />
  10. <category android:name="android.intent.category.LAUNCHER" />
  11. </intent-filter>
  12. <intent-filter>
  13. <!-- 配置action -->
  14. <action android:name="android.intent.action.SEARCH" />
  15. </intent-filter>
  16. <!-- 指定搜索的配置文件 -->
  17. <meta-data android:name="android.app.searchable"
  18. android:resource="@xml/searchable" />
  19. </activity>
  20. <meta-data android:name="android.app.default_searchable"
  21. android:value=".SearchableMusicActivity" />
复制代码


通过以上三步就能实现搜索对话框了。

接下来会讲到搜索关键字提示,先到这里。

-----------------EOF-------------------

1.创建搜索建议提供者
Android已经为我们创建了一个默认的,我们只需要继承 SearchRecentSuggestionProvider 就稍做修改就可以了。
见代码:
  1. import android.content.SearchRecentSuggestionsProvider;

  2. /**
  3. * 搜索提示

  4. * @author Hanguo
  5. * http://t.sina.com.cn/halzhang
  6. * @version 2011-1-5上午11:51:39
  7. */
  8. public class SearchSuggestionsProvider extends SearchRecentSuggestionsProvider {
  9. //记住这个哦
  10. public final static String AUTHORITY = "searchprovider";

  11. public final static int MODE = DATABASE_MODE_QUERIES;

  12. public SearchSuggestionsProvider() {
  13. setupSuggestions(AUTHORITY, MODE);
  14. }
  15. }
复制代码

  

2.配置searchable.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:label="@string/search_label" 
  4. android:hint="@string/search_hint"
  5. android:searchSettingsDescription="@string/search_settings_description"
  6. android:searchSuggestAuthority="searchprovider"
  7. android:searchSuggestIntentAction="android.intent.action.SEARCH"
  8. android:searchSuggestThreshold="1"
  9. android:includeInGlobalSearch="true"
  10. android:searchSuggestSelection=" ?"
  11. >
  12. </searchable>
复制代码
参数说明:
android:searchSuggestAuthorith
此属性的值就是SearchSuggestAuthorith中的AUTHORITH了。
android:searchSuggestIntentAction
此属性定义了当我们选中搜索提示的内容时发生的目的动作。
android:searchSuggestThreshold
此属性定义了至少输入几个字符时才会弹出提示
android:includeInGlobalSearch
是否将内容加入android的全局搜索。true,加入。
android:searchSuggestSelection
定义搜索时参数的占位符
原创粉丝点击