android浮动搜索框的配置比较繁琐,需要配置好xml文件才能实现onSearchRequest()方法。

1.配置搜索的XML配置文件​,新建文件searchable.xml,保存在res/xml文件夹中。

1 <?xml version="1.0" encoding="UTF-8"?>2 3 <searchable android:label="@string/search_label"4     android:searchSuggestAuthority="search"5   android:searchSuggestIntentAction="android.intent.action.VIEW"6      xmlns:android="http://schemas.android.com/apk/res/android"7      />

2.新建一个SearchActivity.java,继承Activity,在配置文件manifest.xml中​添加如下信息

 1     <activity 2             android:name=".SearchActivity" 3             android:launchMode="singleTop"  4             android:label="@string/app_name" > 5              6             <intent-filter>   7                 <action android:name="android.intent.action.SEARCH" />    8                 <category android:name="android.intent.category.DEFAULT" />    9             </intent-filter>  10             <!-- 指定上面的searchable.xml文件 -->  11             <meta-data android:name="android.app.searchable" 12                         android:resource="@xml/searchable" />13         </activity>

3.至此,onSearchRequest方法才可以使用,该方法会调出android的浮动搜索框​,java代码如下

 1 public class SearchActivity extends Activity {  2  3     public void onCreate(Bundle savedInstanceState) {  4         super.onCreate(savedInstanceState);  5  6         Intent intent = this.getIntent(); 7  8         if(Intent.ACTION_SEARCH.equals(intent.getAction())) {   9             String query =  intent.getStringExtra(SearchManager.QUERY);  10             doSearch(query);  11         } 12         else{13             onSearchRequested();14 15         }16 17     }18 19     private void doSearch(String queryStr) {  20         //执行真正的查询结果处理 21     } 22 23 }        

更多相关文章

  1. Android-缓存数据保存-通用方法
  2. 关于Base64解码和编码报错的问题
  3. 在Android中通过Java修改文件权限
  4. Android(安卓)内核与标准Linux内核的区别
  5. Android(安卓)Studio系列-签名打包
  6. 浅谈Java中Collections.sort对List排序的两种方法
  7. NPM 和webpack 的基础使用
  8. Python list sort方法的具体使用
  9. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程

随机推荐

  1. @dimen/padding_medium tools:context 都
  2. Android(安卓)studio常见问题
  3. Shape添加阴影效果
  4. HttpURLConnection类的保持session会话
  5. Android(安卓)MediaStore检索视频并播放
  6. MULTI-INTERFACE CONNECTIVITY ON ANDROI
  7. android 自定义PickerView 日期滑动选择
  8. 对android:textAlignmentStyle 属性参数
  9. Android(安卓)Material Design初步认识
  10. MediaPlayer的使用 带有seekBar