android浮动搜索框

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 }        
原文地址:https://www.cnblogs.com/pngcui/p/4335844.html