android内置搜索对话框(浮动搜索)例子

本文详细介绍了如何在Android应用中实现搜索功能,包括配置searchbar、搜索结果处理、保存历史记录等关键步骤。通过XML配置文件设置searchable属性,利用manifest.xml配置搜索结果处理的Activity,实现从其他Activity调用或在同一Activity内处理搜索结果,并通过自定义SearchSuggestionProvider保存搜索历史。同时,文章提供了完整的代码示例,包括主Activity和搜索结果Activity的实现,帮助开发者快速理解并应用到实际项目中。

差点忘了,先上图看效果吧:

步骤:

(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"); } } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值