上面写道自己折腾了好久,才弄了一个自己的搜索框,现在终于找到android自带的搜索框了,赶紧来用一下吧:

1. 如果想启动那个android自己的那个很好看的搜索控件(其实是一个浮动的Activity),只需要一个函数:onSearchRequested();不过这个搜索框也要通过一个xml做配置,xml文件叫searchable.xml(网上都这么叫的,不知道可不可以改名,不过我没改过),放在layout/xml文件夹下:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/searchLabel" android:hint="@string/searchHint"
android:searchSuggestAuthority="com.android.handmap.SuggestionProvider" android:searchSuggestSelection=" ? ">
</searchable>
searchLabel和searchHint字符串必须定义在string.xml下,不可以单独写,searchHint就是你默认在编辑框里的提示文字。android:searchSuggestAuthority="com.android.handmap.SuggestionProvider" android:searchSuggestSelection=" ? "如果你想记录自己的历史关键字并做联想匹配,这两句貌似是必须的,前面一句是你响应搜索结果的那个activity的包名再加上”.SuggestionProvider“,后面那个是做任意匹配的意思,最开始写这个xml的时候不知道后面那句的意思,只写了前面一句,结果搜索框死活不给我匹配,气得我跳脚,后来加了这一句,它才乖乖给我做匹配,哈哈,真好。。。。

2. 说多了,再折回来,现在有了这个搜索的界面,还需要有一个 (并且只有一个)响应搜索结果的activity,在AndroidManifest.xml肯定要定义一个activity,然后再加几句,才能响应搜索结果:
<activity ....
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
/activity>
这样写好后,就可以用你定义的这个activity接收搜索结果了,如果想在整个应用程序中都可以调用搜索框,在所有activity配置好后,再加几句:
<activity ....
/activity>....
<activity ....
/activity>....
<meta-data android:name="android.app.default_searchable"
android:value="你定义接收搜索结果的activity包名" />

3. 现在我们要接收搜索结果了,
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String str = intent.getStringExtra(SearchManager.QUERY);
suggestions.saveRecentQuery(str, null);
doMySearch(str);
}
}
一般如果点搜索按钮后,是相当于新建一个你定义的activity,在onCreate中接收搜索的intent,当你按“返回”键里,会回到没有执行搜索前的一个Activity,如果你只想保留单一的一个activity,可以配置android:launchMode=”singleTop”,这样的话需要在onNewIntent()接收搜索intent。
suggestions的定义:SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
建立一个java文件,这个文件用于启动一个provider记录历史关键字
public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
/**
* Authority
*/
final static String AUTHORITY = "com.android.handmap.SuggestionProvider";
/**
* Mode
*/
final static int MODE = DATABASE_MODE_QUERIES;

public SearchSuggestionProvider() {
super();
setupSuggestions(AUTHORITY, MODE);
}
}
同样,在AndroidManifest.xml需要做相应配置:
<provider android:name="响应搜索的activity包名.SearchSuggestionProvider"
android:authorities="响应搜索的activity包名.SuggestionProvider" />

4. 如果想在搜索框中定义自己的一些参数,可以重写onSearchRequested()
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putInt(”key“, ”your words“);
startSearch(”your words in text“, true, appData, false);
return true;
}
startSearch()函数,参数1:你准备在搜索框中显示的提示信息;参数2:true时关键字高亮显示,false的话光标在关键字之后,参数3:appData为你要传递的信息,从getIntent()中可以通过Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);获取到;参数4:true的话会在全局搜索,false的话只在你保存的历史关键字中搜索。

到现在为止,整个搜索的功能就做好了,再重点说明一下我做搜索框时出的一个问题,我们的应用由很多activity组成,本来以为任意一个activity都可以弹出搜索框,调用onSearchRequested(),然后在我定义的那个activity中接收搜索信息就行了,后来发现,如果在其他界面弹出搜索框,点击搜索按钮后不会调用onNewIntent()函数,只有在接收搜索结果的那个界面调用onSearchRequested(),才能触发onNewIntent()函数,得到我们想传递进来的appData,所以当想要启动搜索框时,需要将当前所有页面finish掉,进入接收结果的Activity再启动onSearchRequested()。

参考文章:
Android系统搜索对话框(浮动搜索框)的使用
android利用数据库实现搜索联想功能

更多相关文章

  1. Android学习问题:关于AlertDialog中自定义布局带有的EditText无法
  2. AsyncTask类的用法
  3. Android歌词秀设计思路(1)SafetyTimer
  4. Android(安卓)自定义View--ProgressBar篇(三)
  5. Macaca自动化测试Android和IOS应用
  6. android 最简单的自定义适配器(BaseAdapter+ListView)
  7. Android(安卓)View构造方法第三参数使用方法详解
  8. android 霓虹灯效果
  9. Android+SpringMVC通信

随机推荐

  1. Android TextView全属性
  2. Android中对Handle机制的理解
  3. android 中的 Broadcast 机制详解
  4. Android Studio开发基础之AutoCompleteTe
  5. Android应用程序用真机调试步骤
  6. Android Studio系列(二)使用Android Studio
  7. android 设置布局动画
  8. Android 2.0 平台的亮点
  9. Android(安卓)如何保持屏幕长亮?
  10. Android开发从入门到精通(项目案例版)——