(转)Android搜索框架

http://www.cnblogs.com/halzhang/archive/2010/09/13/1825145.html
Android是google的产品,所以自然是少不了搜索。先看看Android一些应用中的搜索对话框。
[img]http://images.cnblogs.com/cnblogs_com/halzhang/WindowsLiveWriter/AndroidAndroid_E1EB/s1_thumb.png[/img]图1 Android中的全局搜索

[img]http://images.cnblogs.com/cnblogs_com/halzhang/WindowsLiveWriter/AndroidAndroid_E1EB/s2_thumb.png[/img]图2 联系人搜索

[img]http://images.cnblogs.com/cnblogs_com/halzhang/WindowsLiveWriter/AndroidAndroid_E1EB/s3_thumb.png[/img]图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" />

通过以上三步就能实现搜索对话框了。
[img]http://images.cnblogs.com/cnblogs_com/halzhang/WindowsLiveWriter/AndroidAndroid_E1EB/device_thumb.png[/img]


下面说说如何在搜索的时候弹出搜索历史提示
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
定义搜索时参数的占位符

PS:配置参数不止这些,可以自己看看android的参考手册。

3.配置AndroidManifest.xml
 <provider android:name=".SearchSuggestionsProvider" android:authorities="searchprovider" />


注意authorities的属性值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值