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