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

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

步骤:

(1)配置search bar的相关信息,新建一个位于res/xml下的一个searchable.xml的配置文件

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <searchable  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:hint="@string/searchHint"   
  5.   android:searchMode="showSearchLabelAsBadge"  
  6.     android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"  
  7.     android:searchSuggestSelection=" ? ">  
  8.     
  9. </searchable>  
 

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

专门处理搜索结果,第二种是就在当前Activity就是处理结果的Activity,这配置里包含两种情况,自己可以看代码能分辨出来。

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.android.cbin"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".Main"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.               
  14.             <meta-data android:name="android.app.default_searchable"  
  15.                        android:value=".SearchResultActivity" />  
  16.         </activity>  
  17.     <activity android:name="SearchResultActivity" android:launchMode="singleTop">  
  18.       
  19.         <intent-filter>  
  20.             <action android:name="android.intent.action.SEARCH"></action>  
  21.         </intent-filter>  
  22.         <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>  
  23.     </activity>  
  24. <provider android:name="SearchSuggestionSampleProvider" android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider>  
  25. </application>  
  26.     <uses-sdk android:minSdkVersion="7" />  
  27. </manifest>   
 

(3 )  保存历史记录  
上面authorities指向的都是name中所关联的SearchSuggestionSampleProvider,他是一个
SearchRecentSuggestionsProvider的子类

[java]  view plain copy
  1. package com.android.search;  
  2. import android.content.SearchRecentSuggestionsProvider;  
  3. public class SearchSuggestionSampleProvider extends  
  4.         SearchRecentSuggestionsProvider {  
  5.     final static String AUTHORITY="com.android.search.SearchSuggestionSampleProvider";  
  6.     final static int MODE=DATABASE_MODE_QUERIES;  
  7.       
  8.     public SearchSuggestionSampleProvider(){  
  9.         super();  
  10.         setupSuggestions(AUTHORITY, MODE);  
  11.     }  
  12. }  
 

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

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

[java]  view plain copy
  1. package com.android.search;  
  2. import com.android.search.R;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. public class Main extends Activity implements OnClickListener{  
  10.     /** Called when the activity is first created. */  
  11.     private EditText etdata;  
  12.     private Button btnsearch;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         findview();  
  19.     }  
  20.       
  21.     private void findview(){  
  22.         etdata=(EditText)findViewById(R.id.etdata);  
  23.         btnsearch=(Button)findViewById(R.id.btncall);  
  24.         btnsearch.setOnClickListener(this);  
  25.     }  
  26.     @Override  
  27.     public void onClick(View v) {  
  28.         // TODO Auto-generated method stub  
  29.         onSearchRequested();  
  30.     }  
  31.       
  32.     @Override  
  33.     public boolean onSearchRequested(){  
  34.           
  35.         String text=etdata.getText().toString();  
  36.         Bundle bundle=new Bundle();  
  37.         bundle.putString("data", text);  
  38.           
  39.         //打开浮动搜索框(第一个参数默认添加到搜索框的值)  
  40.         //bundle为传递的数据  
  41.         startSearch("哈哈"false, bundle, false);  
  42.         //这个地方一定要返回真 如果只是super.onSearchRequested方法  
  43.         //不但onSearchRequested(搜索框默认值)无法添加到搜索框中  
  44.         //bundle也无法传递出去  
  45.         return true;  
  46.     }  
  47.       
  48. }  
 

(5) 在本Activity中搜索

[java]  view plain copy
  1. package com.android.search;  
  2. import com.android.search.R;  
  3. import android.app.Activity;  
  4. import android.app.SearchManager;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.provider.SearchRecentSuggestions;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12. public class SearchResultActivity extends Activity implements OnClickListener{  
  13.     private TextView tvquery,tvdata;  
  14.     private Button btnsearch;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState){  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.searchresult);  
  19.           
  20.         tvquery=(TextView)findViewById(R.id.tvquery);  
  21.         tvdata=(TextView)findViewById(R.id.tvdata);  
  22.         btnsearch=(Button)findViewById(R.id.btnSearch);  
  23.         doSearchQuery();  
  24.           
  25.         btnsearch.setOnClickListener(this);  
  26.     }  
  27.       
  28.     public void doSearchQuery(){  
  29.         final Intent intent = getIntent();  
  30.         //获得搜索框里值  
  31.         String query=intent.getStringExtra(SearchManager.QUERY);  
  32.         tvquery.setText(query);  
  33.         //保存搜索记录  
  34.         SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,  
  35.                 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);  
  36.         suggestions.saveRecentQuery(query, null);  
  37.         if(Intent.ACTION_SEARCH.equals(intent.getAction())){  
  38.             //获取传递的数据  
  39.             Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);  
  40.             if(bundled!=null){  
  41.                 String ttdata=bundled.getString("data");  
  42.                 tvdata.setText(ttdata);  
  43.             }else{  
  44.                 tvdata.setText("no data");  
  45.             }  
  46.         }  
  47.     }  
  48.     @Override  
  49.     public void onClick(View v) {  
  50.         // TODO Auto-generated method stub  
  51.         onSearchRequested();  
  52.     }  
  53.       
  54.     @Override  
  55.     public boolean onSearchRequested(){  
  56.           
  57.         startSearch("onNewIntent"falsenullfalse);  
  58.         return true;  
  59.     }  
  60.       
  61.     @Override  
  62.     public void onNewIntent(Intent intent){  
  63.         super.onNewIntent(intent);  
  64.         //获得搜索框里值  
  65.         String query=intent.getStringExtra(SearchManager.QUERY);  
  66.         tvquery.setText(query);  
  67.         //保存搜索记录  
  68.         SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,  
  69.                 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);  
  70.         suggestions.saveRecentQuery(query, null);  
  71.         if(Intent.ACTION_SEARCH.equals(intent.getAction())){  
  72.             //获取传递的数据  
  73.             Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);  
  74.             if(bundled!=null){  
  75.                 String ttdata=bundled.getString("data");  
  76.                 tvdata.setText(ttdata);  
  77.             }else{  
  78.                 tvdata.setText("no data");  
  79.             }  
  80.         }  
  81.     }  
  82. }  


   这里自己多啰嗦几句:

           1.当你输入搜索字符的时候,系统会自动进行搜索,搜索是在你通过android:searchSuggestAuthority关联的数据             库中进行搜索的。

      2.当你输入完搜索字符,点搜索按键或者按回车,会调用“onNewIntent”,在这里你可以从intent中取到你输入的字              符,然后用这个作为关键字到数据库中进行搜索

       



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值