差点忘了,先上图看效果吧:
步骤:
(1)配置search bar的相关信息,新建一个位于res/xml下的一个searchable.xml的配置文件
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/searchHint"
android:searchMode="showSearchLabelAsBadge"
android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"
android:searchSuggestSelection=" ? ">
</searchable>
(2) manifest.xml配置,搜索结果处理的Activity将出现两种情况,一种是从其他Activity中的search bar打开一个Activtiy
专门处理搜索结果,第二种是就在当前Activity就是处理结果的Activity,这配置里包含两种情况,自己可以看代码能分辨出来。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.cbin"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />
</activity>
<activity android:name="SearchResultActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH"></action>
</intent-filter>
<meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>
</activity>
<provider android:name="SearchSuggestionSampleProvider" android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
(3 ) 保存历史记录
上面authorities 指向的 都是name中所关联的SearchSuggestionSampleProvider,他是一个
SearchRecentSuggestionsProvider的子类
package com.android.search;
import android.content.SearchRecentSuggestionsProvider;
public class SearchSuggestionSampleProvider extends
SearchRecentSuggestionsProvider {
final static String AUTHORITY="com.android.search.SearchSuggestionSampleProvider";
final static int MODE=DATABASE_MODE_QUERIES;
public SearchSuggestionSampleProvider(){
super();
setupSuggestions(AUTHORITY, MODE);
}
}
(4)为了能够使用search bar 我们必须重写Activity的onSearchRequested的方法,在界面上启动一个search bar
但是这个动作不会自动触发,必须通过一个按钮或者菜单的点击事件触发;
package com.android.search;
import com.android.search.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private EditText etdata;
private Button btnsearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
}
private void findview(){
etdata=(EditText)findViewById(R.id.etdata);
btnsearch=(Button)findViewById(R.id.btncall);
btnsearch.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onSearchRequested();
}
@Override
public boolean onSearchRequested(){
String text=etdata.getText().toString();
Bundle bundle=new Bundle();
bundle.putString("data", text);
//打开浮动搜索框(第一个参数默认添加到搜索框的值)
//bundle为传递的数据
startSearch("哈哈", false, bundle, false);
//这个地方一定要返回真 如果只是super.onSearchRequested方法
//不但onSearchRequested(搜索框默认值)无法添加到搜索框中
//bundle也无法传递出去
return true;
}
}
(5) 在本Activity中搜索
package com.android.search;
import com.android.search.R;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SearchResultActivity extends Activity implements OnClickListener{
private TextView tvquery,tvdata;
private Button btnsearch;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.searchresult);
tvquery=(TextView)findViewById(R.id.tvquery);
tvdata=(TextView)findViewById(R.id.tvdata);
btnsearch=(Button)findViewById(R.id.btnSearch);
doSearchQuery();
btnsearch.setOnClickListener(this);
}
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");
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onSearchRequested();
}
@Override
public boolean onSearchRequested(){
startSearch("onNewIntent", false, null, false);
return true;
}
@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");
}
}
}
}