搜索框的实现

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

 

01 <searchable xmlns:android=http://schemas.android.com/apk/res/android
02 
03  <!-- label为搜索框上方的文本,hint搜索框里面的提示文本,显示label -->
04     android:label="@string/search_label"
05     android:hint="@string/search_hint"
06     android:searchMode="showSearchLabelAsBadge"
07 
08  <!-- 语音搜索配置 -->
09     android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
10     android:voiceLanguageModel="free_form"
11     android:voicePromptText="@string/search_invoke"
12 
13   
14     <!-- 配置搜索建议,配置错误将不会显示,这里的searchSuggestAuthority的值必须是
15     继承自SearchRecentSuggestionsProvider的完整路径名 -->
16 
17    android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"
18     android:searchSuggestSelection=" ? "
19/>

  manifest.xml配置,搜索结果处理的Activity将出现两种情况,一种是从其他Activity中的search bar打开一个Activtiy专门处理搜索结果,第二种是就在当前Activity就是处理结果的Activity,先介绍第一种配置:

01 <activity android:name="SearchResultActivity">
02<intent-filter>
03 <action android:name="android.intent.action.SEARCH"></action>
04</intent-filter>
05 
06     
07     <!-- 指定上面的searchable.xml文件 -->
08 
09    <meta-data android:resource="@xml/searchable"
10 
11          android:name="android.app.searchable"></meta-data>
12 
13</activity>
14<!-- 为了使每一个Activity都能使用search bar,一定要将这个标签放到启动Activity中,里面的value指定 的是前面的搜索结果Activity-->
15 <meta-data android:name="android.app.default_searchable"
16                        android:value=".SearchResultActivity"
17/>

搜索建议在manifest.xml中相关的配置  

1<!--之前searchable.xml中有一个searchSuggestAuthority的值其实和这里的
2authorities指向的都是name中所关联的SearchSuggestionSampleProvider,他是一个
3SearchRecentSuggestionsProvider的子类-->
4 
5 <provider android:name="SearchSuggestionSampleProvider"
6 
7 android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider>

为了能够使用search bar 我们必须重写Activity的onSearchRequested的方法,在界面上启动一个search bar 但是这个动作不会自动触发,必须通过一个按钮或者菜单的点击事件触发;  

01@Override
02     public boolean onSearchRequested(){
03          
04         String text=etdata.getText().toString();
05         Bundle bundle=new Bundle();
06         bundle.putString("data", text);
07          
08         //打开浮动搜索框(第一个参数默认添加到搜索框的值)
09         //bundle为传递的数据
10         startSearch("mm", false, bundle, false);
11         //这个地方一定要返回真 如果只是super.onSearchRequested方法不但
12      //onSearchRequested(搜索框默认值)无法添加到搜索框中,bundle也无法传递出去
13 
14return
15 true;
16     }

接收query和bundle、保存query值(即搜索建议的列表值)  

01 public void doSearchQuery(){
02         final Intent intent = getIntent();
03         //获得搜索框里值
04         String query=intent.getStringExtra(SearchManager.QUERY);
05         tvquery.setText(query);
06         //保存搜索记录
07         SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,
08                 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
09         suggestions.saveRecentQuery(query, null);
10         if(Intent.ACTION_SEARCH.equals(intent.getAction())){
11             //获取传递的数据
12             Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
13             if(bundled!=null){
14                 String ttdata=bundled.getString("data");
15                 tvdata.setText(ttdata);
16 
17             }else{
18                 tvdata.setText("no data");
19             }
20         }
21     }

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

01@Override
02     public void onNewIntent(Intent intent){
03         super.onNewIntent(intent);
04         //获得搜索框里值
05         String query=intent.getStringExtra(SearchManager.QUERY);
06         tvquery.setText(query);
07         //保存搜索记录
08         SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,
09                 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
10         suggestions.saveRecentQuery(query, null);
11         if(Intent.ACTION_SEARCH.equals(intent.getAction())){
12             //获取传递的数据
13             Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
14             if(bundled!=null){
15                 String ttdata=bundled.getString("data");
16                 tvdata.setText(ttdata);
17 
18             }else{
19                 tvdata.setText("no data");
20             }
21         }
22     }

  相关知识:上面讲到了将最近的搜索值添加到搜索建议中,但却没有提到如果清理搜索建议中的值,与保存相似,SearchRecentSuggestion对象提供了一个clearHistory()方法  

1 private void clearSearchHistory() {
2         SearchRecentSuggestions suggestions =
3 new SearchRecentSuggestions(this,
4                 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
5         suggestions.clearHistory();
6     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值