【起航计划 032】2015 起航计划 Android APIDemo的魔鬼步伐 31 App->Search->Invoke Search 搜索功能 Search Dialog SearchView SearchRecentSuggestions
Search (搜索)是Android平台的一个核心功能之一,用户可以在手机搜索在线的或是本地的信息。Android平台为所有需要提供搜索或是查询功能的应用提 供了一个统一的Search Framework来帮助实现Search功能。Search Framework的UI可以有两种形式:
- 屏幕顶部的Search Dialog:如Google Map提供的搜索条。
- 可以嵌到ContentView中的SearchView,应用可以将它放在屏幕上任何地方。
不管采用那种UI,Android系统都可以通过向某个指定Activity发送需要查询的内容来帮助应用实现查询功能。同时Android也支持查询提示,如下图所示:
除此之外,Android查询UI可以支持:
- 语音查询
- 根据用户输入提供查询提示列表
- 支持应用自定义查询提示列表来匹配用户输入
- 在系统全局搜索(System-wide Quick Search Box)提供你的应用相关的查询提示列表
Invoke Search介绍了如何使用Search Framework 并采用Search dialog 的方式在屏幕顶部显示查询条。下面结合例子介绍使用Search Framework的一般步骤:
Create a Search Interface
例采用屏幕顶部Search Dialog的方式。在这种方式下,Android操作系统接管所有Search Dialog的事件,当用户提交查询后,Android系统将给支持的用来处理查询的Activity发送消息。Search Dialog可以提供查询提示列表来匹配用户输入。
用户提交查询后,Android系统构造一个Intent并把用户的查询内容放在这个Intent中。然后Android启动你定义的用来处理用户查询的 Activity(称为Searchable Activity),并把这个Intent发给该Activity。为了能够使用Android系统提供的Search Framework.需要以下几步:
1. Creating a Searchable Configuration
首先定义一个Searchable configuration,用于描述Search Dialog 的一些属性,该描述文件按惯例通常命名为searchable.xml 并定义在/res/xml 目录下:
<!-- The attributes in this XML file provide configuration information -->
<!-- for the Search Manager. --> <searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="@string/search_invoke" android:searchSuggestAuthority="com.example.android.apis.SuggestionProvider"
android:searchSuggestSelection=" ? "
/>
只有android:label是必须的,一般定义为应用程序的名称。尽管不是必须的,一般也会定义android:hint。这个属性定义查询框没有任 何输入时的背景文字。如上图中的”Search the dictionary” 。本例中为“Search Demo Hint”来提示用户可以输入的内容。
2. Creating a Searchable Activity
一个”Searchable Activity”就是一个可以用来处理Search Query 的Activity。和一般的Activity没有太大分别。当用户提交查询后,Android会给这个“Searchable Activity”发送一个Intent包含有用户查询内容,同时这个Intent 含有ACTION_SEARCH action。
由于可以在任何一个Activity中使用Search Dialog或是SearchView,Android需要知道哪个Activity是“Searchable Activity”,这就需要在AndroidManifest.xml中来定义“Searchable Activity”。
本例中 SearchQueryResults 定义为“Searchable Activity”,它在AndroidManifest.xml中定义为:
<!-- This activity represents the "search" activity in your application, in which -->
<!-- search results are gathered and displayed. --> <activity android:name=".app.SearchQueryResults"
android:label="@string/search_query_results">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> <!-- This intent-filter identifies this activity as "searchable" --> <intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> <!-- This metadata entry provides further configuration details for searches -->
<!-- that are handled by this activity. --> <meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
Searchable Activity 需要在Intent-filter中指定android.intent.action.SEARCH,并在<meta-data>部分指定 searchable configuration (指向res/xml/searchable.xml)
SearchQueryResults 用来处理用户查询请求,本例为简单起见,只是在屏幕上显示用户查询请求的内容。
它用来处理查询请求Intent的代码如下:
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onNewIntent()");
}
else {
mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent");
}
如果请求的Action为ACTION_SEARCH,表明该Activity是由Search Dialog触发的,则调用doSearchQuery来显示用户查询内容。此外这个Activity也可以从Launcher启动,此时Action不 含ACTION_SEARCH。
Using the Search Dialog
Search Dialog 先为屏幕上方的浮动窗口,缺省为不可见的。只有当调用onSearchRequested()或是用户按“Search”键时(不是所有设备都有Search钮,在模拟器上可以用F5)Search Dialog才会显示。
为了使用Search Dialog,我们在AndroidManifest.xml定义了Searchable Activity: SearchQueryResults。 如果此时直接运行SearchQueryResults,在模拟器上按F5,将会在屏幕上方显示Search Dialog。
如果现在Invoke Search (SearchInvoke)Activity也可以使用Search Dialog, 也需要在AndroidManifest.xml做些说明:
<activity android:name=".app.SearchInvoke"
android:label="@string/search_invoke">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> <!-- This metadata entry causes .app.SearchQueryResults to be the default context -->
<!-- whenever the user invokes search while in this Activity. -->
<meta-data android:name="android.app.default_searchable"
android:value=".app.SearchQueryResults" /> <!-- This is not the typical way to define android.app.default_searchable, -->
<!-- and we show it here only because we wish to confine the search demo to this -->
<!-- section of the ApiDemos application. --> <!-- For typical applications, it's simpler to define android.app.default_searchable -->
<!-- just once, at the application level, where it serves as a default for all of -->
<!-- the Activities in your package. -->
</activity>
这时按下 onSearchRequest() 或是 “Search”键就显示Search Dialog,按查询键后,将会在SearchQueryResults显示用户输入的查询内容:
此外我们看到这Search Dialog下提供了最近用户输入作为输入提示,如果里面含有敏感信息,可以清除这个列表。
private void clearSearchHistory() {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}
【起航计划 032】2015 起航计划 Android APIDemo的魔鬼步伐 31 App->Search->Invoke Search 搜索功能 Search Dialog SearchView SearchRecentSuggestions的更多相关文章
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考
01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:
- 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller
Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...
- 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder
本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...
随机推荐
- Hibernate 环境配置和依赖添加(使用java web和普通javaSE工程)
1.Hibernate依赖包的添加 File---->Project Structure,按照如图所示操作,导入所依赖的jar包. 2.生成hibernate.hbm.xml的配置文件 (1)点 ...
- rest-assured之获取响应数据(Getting Response Data)
我们使用rest-assured可以获得响应内容,比如:我们发起一个get请求 get("/lotto") 并且获得响应内容,我们有多种方式可以实现: // 通过流的方式获得响应内 ...
- Django 中 admin 的执行流程
Django 中 admin 的执行流程 1 循环加载执行所有已经注册的 app 中的 admin.py 文件 def autodiscover(): autodiscover_modules('ad ...
- Fleury算法求欧拉路径
分析: 小Ho:这种简单的谜题就交给我吧! 小Hi:真的没问题么? <10分钟过去> 小Ho:啊啊啊啊啊!搞不定啊!!!骨牌数量一多就乱了. 小Hi:哎,我就知道你会遇到问题. 小Ho:小 ...
- 解决分批次调用 jsonp 接口的 callback 会报错问题
当我们分批次调用同一个jsonp接口时,会有一定机率同时调用,而jsonp的callback不支持同时调用, 会报错,所以当我们在分批次调用同一jsonp接口时,最好在callback后加个变量值,总 ...
- Ubuntu下的UNITY和GNOME界面
[转自] http://www.tuicool.com/articles/nUbMVbU 从Ubuntu 11.04后,UNITY就作为默认界面来推广.如果用户需要体验GNOME 3,还需要用户自己安 ...
- PIE SDK打开长时间序列数据
1. 功能简介 时间序列数据(time series data)是在不同时间上收集到的数据,这类数据是按时间顺序收集到的,用于所描述现象随时间变化的情况.当前随着遥感卫星技术日新月异的发展,遥感卫星的 ...
- python 网页爬取数据生成文字云图
1. 需要的三个包: from wordcloud import WordCloud #词云库 import matplotlib.pyplot as plt #数学绘图库 import jieba; ...
- windows删除指定日期前的文件
@ echo offforfiles /p .\ /s /m 2008*.* /d -7 /c "cmd /c echo @file>>.\del.txt"forfil ...
- Nginx设置日志分割方法
目标:nginx cronolog日志分割配置文档,每分钟分割一次NGINX访问日志. 大体步骤如下: 1.nginx日志配置 access_log /var/log/nginx/access.log ...