017、SearchManager的使用
2013-09-27 21:43
405 查看
SearchManager是Android提供搜索的API
使用SearchManager对象,必须先在AndroidManifest.xml文件里面编写<intent-filter>,使之可以过滤“android.intent.action.SEARCH”广播信息,再在应用程序中建立SearchManager对象。
在AndroidManifest.xml文件中的配置:
其中,引用的android:resource="@xml/searchable" 文件如下:
在应用程序代码中的使用:
上例是是搜索本地数据,也可以实现对网络数据的搜索,代码如下:
使用SearchManager对象,必须先在AndroidManifest.xml文件里面编写<intent-filter>,使之可以过滤“android.intent.action.SEARCH”广播信息,再在应用程序中建立SearchManager对象。
在AndroidManifest.xml文件中的配置:
<activity android:name="com.example.ex_4_29_searchmanager.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity>
其中,引用的android:resource="@xml/searchable" 文件如下:
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/search_label" />
在应用程序代码中的使用:
// 需要在AndroidManifest.xml文件里先添加SEARCH的intent-filter Intent intent = getIntent(); // 取得当按下搜索时的Intent if (Intent.ACTION_SEARCH.equals(intent.getAction())) { // 取得欲搜索的字符串 String extra = intent.getStringExtra(SearchManager.QUERY); queryPicture(extra); } // 设置后,点击键盘会弹出搜索框 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
上例是是搜索本地数据,也可以实现对网络数据的搜索,代码如下:
String trim = ((EditText) findViewById(R.id.et)).getText() .toString().trim(); if (TextUtils.isEmpty(trim)) { Toast.makeText(MainActivity.this, "得输入内容才可以搜索哦", 0).show(); } else { //取得网页搜索的Intent Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //放入所要搜索的文字 intent.putExtra(SearchManager.QUERY, trim); Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA); if(appData!=null){ intent.putExtra(SearchManager.APP_DATA, appData); } startActivity(intent); }
相关文章推荐
- 如何快速屏蔽触摸
- 2014腾讯校园招聘笔试题
- 很酷的结合----芒果和machine learning的故事
- android 分享功能
- 杭电1058
- 杭电1058
- Redis-主从复制
- CoreAnimation编程指南(五)图层内容
- 016、AppWidgetProvider桌面小控件
- UDP和TCP协议的各种应用和应用层协议
- Nagios监控Oralce
- 使用多线程和sleep函数生成字符串的伪随机排列(阿里巴巴2014武汉站一道题)
- 病毒交叉感染
- 经典PS教程
- CoreAnimation编程指南(四)图层树结构
- 015、SlidingDrawer 隐藏式抽屉
- Redis-持久化
- 使用disk genius合并C盘和D盘
- struts2中select的使用
- VirtualBox 出现错误 VT-x features locked or unavailable in MSR.