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】android开发---实现屏幕旋转的两种方法
  2. Android-缓存数据保存-通用方法
  3. 在Android中通过Java修改文件权限
  4. Android 中 ListView Adapter getView 被多次调用问题 解决方法
  5. Android开发便签9:在android资源文件中定义字符串数组
  6. java/android 统计文件夹大小及删除文件夹下所有文件和路径
  7. Android监听来电和去电的实现方法
  8. Android 屏幕的旋转 onConfigurationChanged方法
  9. 查看android里的数据库的内容的方法

随机推荐

  1. Android Support Library
  2. Android 操作SQLite数据库(初步)-在程序中
  3. Android跨进程通信——Activity
  4. Android BackgroundViewPager:类似桌面背
  5. android -------- LiveDataBus的使用
  6. Android Q(10) SystemUI添加快速设置按钮
  7. Android RIL结构分析与移植
  8. (翻译) Backward Compatibility for Appl
  9. android ftp客户端简单实现
  10. Android中EditText输入框焦点从文字前面