免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2794 | 回复: 0
打印 上一主题 下一主题

Android SearchBar 搜索框介绍SearchManager [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-14 13:22 |只看该作者 |倒序浏览
Android SearchBar 搜索框介绍SearchManager



浮动搜索框的使用其实并不难,而是在于它的配置非常之繁琐,对于它的使用主要是方便开发者对于程序中有搜索业务时,更好的设计UI

SearchManager具体使用步骤如下:

(1)配置search bar的相关信息,新建一个位于res/xml下的一个searchable.xml的配置文件,如默认值、是否有搜索建议或者语音搜索。

  1. 01.<searchable xmlns:android=http://schemas.android.com/apk/res/android

  2. 02.  <!-- label为搜索框上方的文本,hint搜索框里面的提示文本,显示label -->

  3. 03.    android:label="@string/search_label"

  4. 04.    android:hint="@string/search_hint"

  5. 05.    android:searchMode="showSearchLabelAsBadge"

  6. 06.   

  7. 07.  <!-- 语音搜索配置 -->

  8. 08.    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"

  9. 09.    android:voiceLanguageModel="free_form"

  10. 10.    android:voicePromptText="@string/search_invoke"

  11. 11.

  12. 12.    <!-- 配置搜索建议,配置错误将不会显示,这里的searchSuggestAuthority的值必须是

  13. 13.    继承自SearchRecentSuggestionsProvider的完整路径名 -->

  14. 14.   android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"

  15. 15.    android:searchSuggestSelection=" ? "

  16. 16./>
复制代码
(2) manifest.xml配置,搜索结果处理的Activity将出现两种情况,一种是从其他Activity中的search bar打开一个Activtiy

专门处理搜索结果,第二种是就在当前Activity就是处理结果的Activity,先介绍第一种配置:

  1. 01.<activity android:name="SearchResultActivity">

  2. 02.   

  3. 03.        <intent-filter>

  4. 04.            <action android:name="android.intent.action.SEARCH"></action>

  5. 05.        </intent-filter>

  6. 06.

  7. 07.     

  8. 08.     <!-- 指定上面的searchable.xml文件 -->

  9. 09.   <meta-data android:resource="@xml/searchable"

  10. 10.         android:name="android.app.searchable"></meta-data>

  11. 11.

  12. 12.    </activity>
复制代码
<!-- 为了使每一个Activity都能使用search bar,一定要将这个标签放到启动Activity中,里面的value指定 的是前面的搜索结果Activity--> <meta-data android:name="android.app.default_searchable"
                       android:value=".SearchResultActivity" />(3)搜索建议在manifest.xml中相关的配置<!--之前searchable.xml中有一个searchSuggestAuthority的值其实和这里的
authorities指向的都是name中所关联的SearchSuggestionSampleProvider,他是一个
SearchRecentSuggestionsProvider的子类-->
<provider android:name="SearchSuggestionSampleProvider"
  android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider>
上面authorities指向的都是name中所关联的SearchSuggestionSampleProvider,他是一个
SearchRecentSuggestionsProvider的子类
  1. 01.public class SearchSuggestionSampleProvider extends

  2. 02.        SearchRecentSuggestionsProvider {

  3. 03.

  4. 04.    final static String AUTHORITY="com.android.cbin.SearchSuggestionSampleProvider";

  5. 05.    final static int MODE=DATABASE_MODE_QUERIES;

  6. 06.   

  7. 07.    public SearchSuggestionSampleProvider(){

  8. 08.        super();

  9. 09.        setupSuggestions(AUTHORITY, MODE);

  10. 10.    }

  11. 11.}
复制代码
(4)为了能够使用search bar 我们必须重写Activity的onSearchRequested的方法,在界面上启动一个search bar

但是这个动作不会自动触发,必须通过一个按钮或者菜单的点击事件触发;

  1. 01.@Override

  2. 02.    public boolean onSearchRequested(){

  3. 03.        

  4. 04.        String text=etdata.getText().toString();

  5. 05.        Bundle bundle=new Bundle();

  6. 06.        bundle.putString("data", text);

  7. 07.        

  8. 08.        //打开浮动搜索框(第一个参数默认添加到搜索框的值)

  9. 09.        //bundle为传递的数据

  10. 10.        startSearch("mm", false, bundle, false);

  11. 11.        //这个地方一定要返回真 如果只是super.onSearchRequested方法不但

  12. 12.     //onSearchRequested(搜索框默认值)无法添加到搜索框中,bundle也无法传递出去

  13. 13.        return true;

  14. 14.    }
  15. 复制代码(5)接收query和bundle、保存query值(即搜索建议的列表值)


  16. 01.public void doSearchQuery(){

  17. 02.        final Intent intent = getIntent();

  18. 03.        //获得搜索框里值

  19. 04.        String query=intent.getStringExtra(SearchManager.QUERY);

  20. 05.        tvquery.setText(query);

  21. 06.        //保存搜索记录

  22. 07.        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,

  23. 08.                SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);

  24. 09.        suggestions.saveRecentQuery(query, null);

  25. 10.        if(Intent.ACTION_SEARCH.equals(intent.getAction())){

  26. 11.            //获取传递的数据

  27. 12.            Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);

  28. 13.            if(bundled!=null){

  29. 14.                String ttdata=bundled.getString("data");

  30. 15.                tvdata.setText(ttdata);

  31. 16.

  32. 17.            }else{

  33. 18.                tvdata.setText("no data");

  34. 19.            }

  35. 20.        }

  36. 21.    }
复制代码
之前说到了处理结果的Activity将可能出现的两种情况的两种,现在就处理第二种状况,就是假如invoke search bar的Activity同时也是处理搜索结果的Activity,如果按照之前的方式处理则会出现一种情况,搜索一次就实例化一次Activity,当按返回键的时候会发现老是同一个Activity,其实为了使它只有一个实例化对象,只需简单的配置和代码就能实现第一:在处理搜索结果Activity的manifest.xml中添加android:launchMode="singleTop"属性第二:重写Activity的onNewIntent(Intent intent)
  1. 01.@Override

  2. 02.    public void onNewIntent(Intent intent){

  3. 03.        super.onNewIntent(intent);

  4. 04.        //获得搜索框里值

  5. 05.        String query=intent.getStringExtra(SearchManager.QUERY);

  6. 06.        tvquery.setText(query);

  7. 07.        //保存搜索记录

  8. 08.        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,

  9. 09.                SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);

  10. 10.        suggestions.saveRecentQuery(query, null);

  11. 11.        if(Intent.ACTION_SEARCH.equals(intent.getAction())){

  12. 12.            //获取传递的数据

  13. 13.            Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);

  14. 14.            if(bundled!=null){

  15. 15.                String ttdata=bundled.getString("data");

  16. 16.                tvdata.setText(ttdata);

  17. 17.

  18. 18.            }else{

  19. 19.                tvdata.setText("no data");

  20. 20.            }

  21. 21.        }

  22. 22.    }
复制代码
相关知识:上面讲到了将最近的搜索值添加到搜索建议中,但却没有提到如果清理搜索建议中的值,与保存相似,SearchRecentSuggestion对象提供了一个clearHistory()方法
  1. 01.private void clearSearchHistory() {

  2. 02.        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,

  3. 03.                SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);

  4. 04.        suggestions.clearHistory();

  5. 05.    }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP