经过以下几个部分的实现和配置,Android内建的搜索框架就可以在你的应用中方便使用了。

1 在需要显示search ui界面的activity中调用search的代码
  1. /** Handle "search" title-bar action. */
  2. public void onSearchClick(View v) {
  3. onSearchRequested();
  4. }

  5. /** do the search **/
  6. @Override
  7. public boolean onSearchRequested() {
  8. Bundle appDataBundle = new Bundle();
  9. appDataBundle.putString("param1", "test");
  10. startSearch("请输入搜索的关键词", true, appDataBundle, false);
  11. return true;
  12. }
复制代码
2 创建xml/searchable.xml 对search的配置
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright 2011 xxx Inc.
  4. -->
  5. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:label="@string/search_label"
  7. android:hint="@string/search_hint"
  8. <!-- Authority设置成对应provider的全称 -->
  9. android:searchSuggestAuthority="com.xxx.alimobile.util.SearchSuggestionsProvider"
  10. android:searchSuggestIntentAction="android.intent.action.SEARCH"
  11. android:searchSuggestThreshold="0"
  12. android:searchSuggestSelection=" ? " />
复制代码 3 在AndroidManifest.xml中,给处理Search请求的页面加入intent过滤器和searchable配置文件
  1. <activity
  2. android:name=".ui.WebViewActivity"
  3. android:theme="@style/Theme.AliMobile"
  4. android:screenOrientation="portrait"
  5. android:configChanges="orientation|keyboard|keyboardHidden">
  6. <intent-filter>
  7. <action android:name="android.intent.action.SEARCH" />
  8. </intent-filter>
  9. <meta-data
  10. android:name="android.app.searchable"
  11. android:resource="@xml/searchable" />
  12. </activity>
复制代码 4 判断是不是action_search intent事件发起的,然后处理。
  1. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  2. String queryString = intent.getStringExtra(SearchManager.QUERY);
  3. strUrl = "http://search1.wap.taobao.com/s/search.htm?q=" + queryString;
  4. }
复制代码
后面还剩下suggestion的配置, 参考此篇技术文章:
http://www.apkbus.com/android-14572-1-1.html
5 SearchSuggestionProvider类,继承自android.content.SearchRecentSuggestionsProvider,直接使用sdk中的简单实现。
  1. import android.content.SearchRecentSuggestionsProvider;

  2. public class SearchSuggestionsProvider extends SearchRecentSuggestionsProvider {
  3. public final static String AUTHORITY = "com.xxx.alimobile.util.SearchSuggestionsProvider";
  4. public final static int MODE = DATABASE_MODE_QUERIES;

  5. public SearchSuggestionsProvider() {
  6. setupSuggestions(AUTHORITY, MODE);
  7. }
  8. }
复制代码
6 在第4步基础上加入保存功能,每次使用搜索后调用provider进行保存搜索的字段。
  1. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  2. String queryString = intent.getStringExtra(SearchManager.QUERY);
  3. strUrl = "http://search1.wap.taobao.com/s/search.htm?q=" + queryString;
  4. // SAVE THE SEARCH QUERY
  5. SearchRecentSuggestions suggestions =
  6. new SearchRecentSuggestions(
  7. this, SearchSuggestionsProvider.AUTHORITY,
  8. SearchSuggestionsProvider.MODE);
  9. suggestions.saveRecentQuery(queryString, null);
  10. } else {
  11. strUrl = (String) intent.getExtras().get("goUrl");
  12. }
复制代码 7 AndroidManifest.xml中加入provider声明。
  1. <provider
  2. android:name=".util.SearchSuggestionsProvider"
  3. android:authorities="com.xxx.alimobile.util.SearchSuggestionsProvider">
  4. </provider>
复制代码

更多相关文章

  1. Android 代码混淆及反编译方法
  2. 代码控制一段时间只触发一次事件(防止多次点击) Android
  3. Android修改字体样式的示例代码
  4. google android最新源代码镜像(git.oschina.net)
  5. Android Studio使用Lint进行代码检查
  6. 初学Android做计时器和代码
  7. android 源代码构建和运行cts
  8. 实现TextView的垂直滚动以及通过代码设置TextView滚动无法显示滚
  9. Android Studio使用XML样式在JAVA代码中的使用(使用java代码调节x

随机推荐

  1. 苹果Mac如何关闭右上角的Siri显示?
  2. Cisco ISE如何关机-----ISE的关机方法和
  3. 从事运维工作是一种怎样的体检?
  4. 苏宁大数据离线任务开发调度平台实践:任务
  5. 逻辑复制-更改复制标识(REPLICA IDENTITY)
  6. Prometheus 之 Kubernetes节点的监控
  7. 帆软和思迈特软件Smartbi产品的详细对比
  8. Linux运维入门教程06-04 (硬盘分区、格式
  9. 百家号在线视频编辑器的技术演进
  10. python3函数中lambda/filter/map/reduce