现在的位置: 首页 > 综合 > 正文

Android的搜索框

2016年09月27日 ⁄ 综合 ⁄ 共 3381字 ⁄ 字号 评论关闭

现在终于找到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()。


原文链接:http://qiangwei27.blog.163.com/blog/static/44899053201071854439590/


抱歉!评论已关闭.