现在的位置: 首页 > 综合 > 正文

Searchable配置文件详解(上)

2013年10月15日 ⁄ 综合 ⁄ 共 6675字 ⁄ 字号 评论关闭

文章来源:http://developer.android.com/guide/topics/search/searchable-config.html

当你开发Searchable应用程序时,你需要一个配置文件来对其进行配置。

关于开发Searchable应用程序的详细内容请参考Searchable》系列。
配置文件应该位于res/xml/下,且一般命名为searchable.xml
语法如下:

        <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="string resource"
        android:hint="string resource"
        android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
        android:searchButtonText="string resource"
        android:inputType="inputType"
        android:imeOptions="imeOptions"
        android:searchSuggestAuthority="string"
        android:searchSuggestPath="string"
        android:searchSuggestSelection="string"
        android:searchSuggestIntentAction="string"
        android:searchSuggestIntentData="string"
        android:searchSuggestThreshold="int"
        android:includeInGlobalSearch=["true" | "false"]
        android:searchSettingsDescription="string resource"
        android:queryAfterZeroResults=["true" | "false"]
        android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
        android:voiceLanguageModel=["free-form" | "web_search"]
        android:voicePromptText="string resource"
        android:voiceLanguage="string"
        android:voiceMaxResults="int"
        >
        <actionkey
            android:keycode="KEYCODE"
            android:queryActionMsg="string"
            android:suggestActionMsg="string"
            android:suggestActionMsgColumn="string" >
    </searchable>
通用属性
android:label必选。它应该和你的程序的名字一样。只有android:includeInGlobalSearch为"true"时,它才多用户可见。它就是在search
settings的searchable列表中所用的名字。
android:hint建议包含该属性。它是用户在输入框键入字符之前,在输入框显示的字符。为个和别的应用程序一致,它应该为
"Search <content-or-product>"的形式,比如"Search songs and artists" or "Search
YouTube"
android:searchMode:它表示当用户选择了一个suggestion后,以何种方式重写输入框中的搜索关键字。如果设置该属性,则不会进行输入框内搜索关键字的重写。

如果设置该属性,则有两种模式可以选:
"queryRewriteFromText":用suggestion Cursor的SUGGEST_COLUMN_TEXT_1列的内容进行重写。
"queryRewriteFromData":这样suggestion
Cursor的
SUGGEST_COLUMN_INTENT_DATA列将用于改写query
text。但是这时
SUGGEST_COLUMN_INTENT_DATA的值必须是对用户可见的URI(比如http形式)或其他格式,不能使用内部的URI。
android:searchButtonText:搜索按钮的文本。默认的是搜索按钮显示的是个搜索图标。如果你的搜索行为是普遍的搜索行为,请不要改变它,即不要设置该属性。
android:inputType:输入的类型。对于大多数搜索应该,都是输入文本来进行搜索,所以一般你不需要设置该属性。关于该属性的可选值请参照inputType
android:imeOptionsSupplies
additional options for the input method. For most searches, in which free-form text is expected, you don't need this attribute. The default IME is "actionSearch" (provides the "search" button instead of a carriage return in the soft keyboard)
一般你不需要设置该属性。关于该属性的可选值请参照 imeOptions 
用于Search suggestion的属性
如果你提供了一个content provider来产生search suggestions,那么就要考虑设置下面的属性。
android:searchSuggestAuthority它必须和你的content
provider的mainfest中的
android:authorities相匹配。
android:searchSuggestPath:当你需要一个Content
Provider为多个searchable activities提供suggestions查询,就需要设置
该属性
它将作为
suggestions query Uri的一部分用于区分是哪个searchable
activities。
android:searchSuggestSelection:它就作为selection参数传入到你的suggetion
content provider的query函数中,它必须包含"?",比如
"query=?",(实际的query将代替"?").如果不设置该属性,那么selectionArgs参数将为null.否则设置了非null的值,那么search
query将做为该数的第一项(其实最多也只有这项).
android:searchSuggestIntentAction:它用于定义当用户选择了suggestion时的intent的action。它可以被SUGGEST_COLUMN_INTENT_ACTION列重写。如果你的所有的suggestions的action都一样,
那么最好使用该属性来定义该intent的action,而不是使用
SUGGEST_COLUMN_INTENT_ACTION列。
android:searchSuggestIntentData:它用于定义当用户选择了suggestion时的intent的data。 它可以通过SUGGEST_COLUMN_INTENT_DATA列来重写。如果你的所有的suggestions的intent的data前面部分都一样,
那么最好
不是使用SUGGEST_COLUMN_INTENT_DATA列,而是使用该属性来定义该intent的data的相同部分,然后再用SUGGEST_COLUMN_INTENT_DATA_ID列来定义不同部分
android:searchSuggestThreshold表示用户至少需要输入了多少个字符才显示suggestion列表。那么在用户输入的字符数小于该值的时候,显示的就是shortcut.SearchableDictionary例子就是这样的。
关于以上suggestion属性的更多内容请参考《Searchable之自定义Suggestions》系列
关于Quick Search Box的属性
android:includeInGlobalSearch:把该属性设置为"true",将使Quick
Search Box
可以对你的searchable进行搜索。但是即使把该属性设置为"true",用户必须通过setting->Search->Searchable
items
Quick Search Box->"g" icon->setting iconQuick
Search Box->menu->search Settings->searchable items
来在Quick
Search Box中开启
你的searchable程序
android:searchSettingsDescription:它是Searchable
items
中对你应用程序进行搜索行为的描述,它应该尽量精简,并说明搜索的内容。比如对于一个音乐程序一般是"Artists,
albums, and tracks",比如对于notepad程序应该是"Saved notes".提供对你的searchable程序的描述是非常重要的,这样用户才知道的你提供的suggestions是针对什么的。当
"android:includeInGlobalSearch"属性为"true"的,你必须设置"android:searchSettingsDescription"属性
android:queryAfterZeroResultsSet
to "true" if you want your content provider to be invoked for supersets of queries that have returned zero results in the past. For example, if your content provider returned zero results for "bo", it should be requiried for "bob". If set to "false", supersets
are ignored for a single session ("bob" does not invoke a requery). This lasts only for the life of the search dialog or the life of the activity when using the search widget (when the search dialog or activity is reopened, "bo" queries your content provider
again). The default value is false.它用于表示在一个搜索关键字的
supersets上次搜索返回结果为0的下,是否还需要对该搜索关键字进行查询。比如我们开始键入了"bo",返回结果为0,那么如果我键入"b",搜索关键字就成了"bob",如果该属性设置为"true",就会再次查询,否则就不查询。这里的不会查询只是在search
dialog或
search widget生命周期内,下次不影响。该属性的默认值是"false".
用于Voice search的属性

如果进行语音搜索,你需要关注<searchable>的以下属性:
android:voiceSearchMode
    Keyword. (Required to provide voice search capabilities.) Enables voice search, with a specific mode for voice search. (Voice search may not be provided by the device, in which case these flags have no effect.) 
下面是3个可选值:
    "showVoiceSearchButton"
Display a voice search button, if voice search is available on the device. If set, then either "launchWebSearch" or "launchRecognizer" must also be set (separated by the pipe | character).
    "launchWebSearch"
The voice search button takes the user directly to a built-in voice web search activity. Most applications don't need this flag, as it takes the user away from the activity in which search was invoked.
    "launchRecognizer"
The voice search button takes the user directly to a built-in voice recording activity. This activity prompts the user to speak, transcribes the spoken text, and forwards the resulting query text to the
searchable activity, just as if the user typed it into the search UI and clicked the search button.
android:voiceLanguageModel
    Keyword. The language model that should be used by the voice recognition system.
下面是2个可选值
    "free_form"
Use free-form speech recognition for dictating queries. This is primarily optimized for English. This is the default.
    "web_search"
Use web-search-term recognition for shorter, search-like phrases. This is available in more languages than "free_form".

更多的可以参考EXTRA_LANGUAGE_MODEL

android:voicePromptText
    String. An additional message to display in the voice input dialog.
android:voiceLanguage
    String. The spoken language to be expected, expressed as the string value of a constants in local (such as "de" for German or "fr" for French). This is needed only if it is different from the current value of Locale.getDefault().
android:voiceMaxResults
    Integer. Forces the maximum number of results to return, including the "best" result which is always provided as the ACTION_SEARCH intent's primary query. Must be 1 or greater. Use EXTRA_RESULTS to get the results from the
intent. If not provided, the recognizer chooses how many results to return.

抱歉!评论已关闭.