a b c d e f g h i j k l m n o p q r s t u v w x y z

Android---浮动搜索框(SearchManager)

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

SearchManager具体使用步骤如下:

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

代码
   <!-- label为搜索框上方的文本,hint搜索框里面的提示文本,显示label -->
android:label
="@string/search_label"
android:hint
="@string/search_hint"
android:searchMode
="showSearchLabelAsBadge"
  <!-- 语音搜索配置 -->
android:voiceSearchMode
="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel
="free_form"
android:voicePromptText
="@string/search_invoke"
 
<!-- 配置搜索建议,配置错误将不会显示,这里的searchSuggestAuthority的值必须是
继承自SearchRecentSuggestionsProvider的完整路径名 -->
    android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"
android:searchSuggestSelection
=" ? "
/>

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

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

代码
<activity android:name="SearchResultActivity">

<intent-filter>
<action android:name="android.intent.action.SEARCH"></action>
</intent-filter>
 
     
     <!-- 指定上面的searchable.xml文件 -->
    <meta-data android:resource="@xml/searchable"
          android:name="android.app.searchable"></meta-data>

</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的子类
代码
public class SearchSuggestionSampleProvider extends
SearchRecentSuggestionsProvider {

final static String AUTHORITY="com.android.cbin.SearchSuggestionSampleProvider";
final static int MODE=DATABASE_MODE_QUERIES;

public SearchSuggestionSampleProvider(){
super();
setupSuggestions(AUTHORITY, MODE);
}
}

 

 

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

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

代码
@Override
public boolean onSearchRequested(){

String text
=etdata.getText().toString();
Bundle bundle
=new Bundle();
bundle.putString(
"data", text);

//打开浮动搜索框(第一个参数默认添加到搜索框的值)
//bundle为传递的数据
startSearch("mm", false, bundle, false);
//这个地方一定要返回真 如果只是super.onSearchRequested方法不但
     //onSearchRequested(搜索框默认值)无法添加到搜索框中,bundle也无法传递出去
return true;
}

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

代码
public void doSearchQuery(){
final Intent intent = getIntent();
//获得搜索框里值
String query=intent.getStringExtra(SearchManager.QUERY);
tvquery.setText(query);
//保存搜索记录
SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(query,
null);
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
//获取传递的数据
Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata
=bundled.getString("data");
tvdata.setText(ttdata);

}
else{
tvdata.setText(
"no data");
}
}
}

 之前说到了处理结果的Activity将可能出现的两种情况的两种,现在就处理第二种状况,就是假如invoke search bar的

Activity同时也是处理搜索结果的Activity,如果按照之前的方式处理则会出现一种情况,搜索一次就实例化一次Activity,当按返回

键的时候会发现老是同一个Activity,其实为了使它只有一个实例化对象,只需简单的配置和代码就能实现

第一:在处理搜索结果Activity的manifest.xml中添加android:launchMode="singleTop"属性

第二:重写Activity的onNewIntent(Intent intent)

代码
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
//获得搜索框里值
String query=intent.getStringExtra(SearchManager.QUERY);
tvquery.setText(query);
//保存搜索记录
SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(query,
null);
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
//获取传递的数据
Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);
if(bundled!=null){
String ttdata
=bundled.getString("data");
tvdata.setText(ttdata);

}
else{
tvdata.setText(
"no data");
}
}
}

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

 

代码
private void clearSearchHistory() {
SearchRecentSuggestions suggestions
= new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}

忘了上效果图:oye

 

 

 

posted @ 2010-11-05 12:21  莴笋炒肉  阅读(20570)  评论(24编辑  收藏  举报