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

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

步骤:

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

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,这配置里包含两种情况,自己可以看代码能分辨出来。

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的子类

1package com.android.search; 2import android.content.SearchRecentSuggestionsProvider; 3public 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

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

1package com.android.search; 2import com.android.search.R; 3import android.app.Activity; 4import android.os.Bundle; 5import android.view.View; 6import android.view.View.OnClickListener; 7import android.widget.Button; 8import android.widget.EditText; 9public 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中搜索

1package com.android.search; 2import com.android.search.R; 3import android.app.Activity; 4import android.app.SearchManager; 5import android.content.Intent; 6import android.os.Bundle; 7import android.provider.SearchRecentSuggestions; 8import android.view.View; 9import android.view.View.OnClickListener; 10import android.widget.Button; 11import android.widget.TextView; 12public 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", false, null, false); 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}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )