Searchable之自定义Suggestions(上)

原文地址:http://developer.android.com/guide/topics/search/adding-custom-suggestions.html<wbr style="line-height:25px"><wbr style="line-height:25px"><div style="line-height:25px"><span style="color:#003366; line-height:25px">当你使用search dialog或search widget的时候,你可以使用自定义的search suggestions,它就来自于你的应用程序。</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">比如,如果你的应用是个字典,你可以给出一些和当前已输入的文本相匹配的words作为search suggestions。这是最好的suggestions方式。</span></div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">因为你可以有效地预测用户想要什么,并提供即时访问它。</span><span style="line-height:25px">图一</span><span style="color:#003366; line-height:25px">就是在search dialog中使用个性化Suggestion的界面。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">图一</span><span style="color:#003366; line-height:25px">:</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px"></span> <div style="line-height:25px"><img alt="自定义的Suggestions(上) - hubingforever - 民主与科学" src="http://developer.android.com/images/search/search-suggest-custom.png" style="line-height:25px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%; margin-top:0px; margin-right:10px; margin-bottom:0px; margin-left:0px"></div> </div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">一旦您提供自定义的建议,您还可以将它们提供给系统的快速搜索框,这样就提供从外部的应用程序访问您的内容的方式。</span></div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">在开始添加自定义的建议前,需要先在你的程序中用search dialog or a search widget实现搜索功能,关于此的详细内容请参考《</span><strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920114101422295/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline">Searchable</a></strong><span style="line-height:25px; color:rgb(0,51,102)">》系列</span> </div> <div style="line-height:25px"><span style="font-size:16px; line-height:28px"><span style="line-height:28px">基本原理</span></span></div> <div style="line-height:25px"> <span style="font-size:13px; line-height:23px">当用户选择了一个custom suggestion,系统将向你的searchable activity发送一个Intent.普通的搜索发送的Intent的是Action为</span><span style="line-height:25px; font-family:monospace"><a rel="nofollow" href="http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ACTION_SEARCH</a>(</span>android.intent.action.SEARCH)<code style="line-height:25px">的Intent,然而这里的Intent的Action却是可自定义的(但是一般还是使用</code><span style="line-height:25px; font-family:monospace"><a rel="nofollow" href="http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">ACTION_VIEW</a></span><span style="line-height:25px; font-family:monospace">(android.intent.action.VIEW)),它还可以包含一些和你选择的suggestion相关的数据。例如示例程序dictionary,当用户选择一个建议,您的应用程序可以立即打开该单词的定义,而不是在dictionary中搜索匹配项。</span> </div> <div style="line-height:25px"><span style="line-height:25px; font-family:monospace">提供自定义的suggestions,需要以下几步:</span></div> <div style="line-height:25px"> <span style="font-family:monospace; line-height:25px"><span style="line-height:25px">A</span>、<span style="color:#0000ff; line-height:25px">实现基本的searchable activity,</span></span><span style="color:#0000ff; line-height:22px">关于此的详细内容请参考</span><span style="color:#003366; line-height:22px">《</span><strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920114101422295/" style="color:rgb(207,121,28); line-height:22px; text-decoration:underline">Searchable</a></strong><span style="line-height:22px; color:rgb(0,51,102)">》</span><span style="line-height:22px"><span style="color:#0000ff; line-height:25px">系列</span></span> </div> <div style="line-height:25px"> <span style="line-height:22px"><span style="line-height:25px">B</span><span style="color:#003366; line-height:25px">、</span></span><span style="line-height:22px"><span style="color:#0000ff; line-height:25px">在searchable的配置文件中指明提供custom suggestions的content provider。</span></span> </div> <div style="line-height:25px"> <span style="line-height:22px"><span style="line-height:25px">C</span><span style="color:#003366; line-height:25px">、</span></span><span style="color:#0000ff; line-height:25px"><span style="line-height:22px">创建一个用于提取你自定义的suggestions的Content Provider,并在manifest中声明该</span>Provider。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">D</span><span style="color:#003366; line-height:25px">、</span><span style="color:#0000ff; line-height:25px">声明<span style="line-height:20px; font-size:13px">当用户选择了一个custom suggestion后,系统向你的searchable activity发送的Intent的Action和data.</span></span> </div> <div style="line-height:25px"><span style="font-size:13px; line-height:23px"></span></div> <div style="line-height:25px"> <span style="font-size:16px; color:#003366; line-height:28px">正如Android系统显示搜索对话框一样,系统也负责您的搜索建议的显示。你所需要的就是提供一个content provide,以便系统从中提取你的</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">搜索建议。</span> </div> <div style="line-height:25px"><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">一旦系统识别到你的Activity是searchable,且它提供了search suggestions。</span></div> <div style="line-height:25px"><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">当用户键入了一个query,下面的procedure将发生:</span></div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,51,102); font-size:16px">A、</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">系统将得到search query text,然后在你的用于提取自定义的</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">suggestions的Provider进行查询。</span> </div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,51,102); font-size:16px">B、你的Content Provider用一个Cursor返回和</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">search query text相关的所有</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">suggestions。</span> </div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,51,102); font-size:16px">C、系统通过Cursor显现和</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">search query text相关的所有自定义</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">suggestions的列表。</span> </div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,51,102); font-size:16px">一旦自定义的</span><span style="line-height:25px; color:rgb(0,51,102); font-size:16px">suggestions被显示,下面的几种情况将发生:</span> </div> <div style="line-height:25px"> <span style="line-height:25px; font-size:16px"><span style="line-height:28px">A</span><span style="color:#003366; line-height:28px">、</span></span><span style="color:#0000ff; line-height:25px"><span style="line-height:25px; font-size:16px">如果用户又键入了,或用户改变了</span><span style="line-height:25px; font-size:16px">search query text,上面提到步骤将再次发生</span><span style="line-height:25px; font-size:16px">。</span></span> </div> <div style="line-height:25px"> <span style="line-height:25px; font-size:16px"><span style="line-height:28px">B</span><span style="color:#003366; line-height:28px">、</span><span style="color:#0000ff; line-height:28px">如果用户点击了搜索键,你的</span></span><span style="line-height:25px; font-size:16px"><span style="color:#0000ff; line-height:28px">search suggestions将被忽略,系统将把普通</span></span><code style="line-height:25px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"><span style="color:#ff6600; line-height:25px">ACTION_SEARCH</span></a></code><span style="color:#0000ff; line-height:25px">intent发送到你的searchable activity</span> </div> <div style="line-height:25px"> <span style="line-height:25px">C</span><span style="color:#003366; line-height:25px">、</span><span style="color:#0000ff; line-height:25px">如果用户选择了一个<span style="line-height:25px; font-size:16px">suggestions,一个携带自定义action和自定义data的Intent将被传送到你的searchable activity,通过该Intent,你的应用程序就可以打开suggestion所对应的具体内容</span></span> </div> <div style="line-height:25px"><span style="line-height:25px; font-size:16px"><span style="line-height:28px">修改searchable配置文件</span></span></div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px"><span style="line-height:25px"><span style="font-size:13px; line-height:23px">为了支持自定义</span></span><span style="line-height:25px; font-size:16px">suggestion,你需要在你的</span>searchable配置文件中<span style="line-height:25px; font-size:16px">把</span></span><code style="line-height:25px"><span style="color:#000080; line-height:25px">android:searchSuggestAuthority</span></code><span style="color:#003366; line-height:25px">属性添加到</span><code style="line-height:25px"><span style="color:#000080; line-height:25px">&lt;searchable&gt;</span></code>中</div> <div style="line-height:25px">比如:</div> <div style="line-height:25px"> <span style="line-height:25px">示例1</span>:</div> <div style="line-height:25px"> <pre style="line-height:25px"><span style="line-height:25px">&lt;?</span><span style="line-height:25px">xml version</span><span style="line-height:25px">=</span><span style="line-height:25px">"1.0"</span><span style="line-height:25px"> encoding</span><span style="line-height:25px">=</span><span style="line-height:25px">"utf-8"</span><span style="line-height:25px">?&gt;</span><span style="line-height:25px"><br style="line-height:25px"></span><span style="line-height:25px">&lt;searchable</span><span style="line-height:25px"> </span><span style="line-height:25px">xmlns:android</span><span style="line-height:25px">=</span><span style="line-height:25px">"http://schemas.android.com/apk/res/android"</span><span style="line-height:25px"><br style="line-height:25px"></span><span style="line-height:25px">android:label</span><span style="line-height:25px">=</span><span style="line-height:25px">"@string/app_label"</span><span style="line-height:25px"><br style="line-height:25px"></span><span style="line-height:25px">android:hint</span><span style="line-height:25px">=</span><span style="line-height:25px">"@string/search_hint"</span><span style="line-height:25px"><br style="line-height:25px"></span><span style="line-height:25px"><span style="line-height:25px">android:searchSuggestAuthority</span><span style="line-height:25px">=</span><span style="line-height:25px">"com.example.MyCustomSuggestionProvider"</span></span><span style="line-height:25px">&gt;</span><span style="line-height:25px"><br style="line-height:25px"></span><span style="line-height:25px">&lt;/searchable&gt;</span></pre> <pre style="line-height:25px"><span style="line-height:25px"><span style="color:#003366; line-height:25px">当然你还可能需要使用其他的一些可选属性,这都取决于你如何把intent和suggestion进行对应,以及你在content provider中进行查询的方式 关于其他的一些可选属性的使用将在后面进行详细介绍。</span></span></pre> </div> </wbr></wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值