---
title: "Searchable之五（使用Search Widget）"
author: "mmdev"
date: "2011-12-08T11:30:00Z"
tags: []
slug: "searchable-search-widget"
draft: false
lastmod: "2011-12-10T16:20:29Z"
id: 1301564
url: "https://mmdev.iteye.com/blog/1301564"
---

九、使用Search Widget  
&lt;wbr style="line-height:25px"&gt;&lt;div style="line-height:25px"&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="line-height:23px; font-size:13px; color:rgb(0,51,102)"&gt;在&lt;/span&gt;&lt;span style="line-height:25px; font-size:13px; color:rgb(0,51,102)"&gt;Android 3.0 或更高版本中，可以使用&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:23px; font-size:13px"&gt;&lt;/span&gt;&lt;code style="line-height:25px; color:rgb(0,51,102)"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/widget/SearchView.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;&lt;span style="font-size:16px; line-height:23px"&gt;SearchView&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;span style="color:#003366; line-height:23px; font-size:13px"&gt;widget.如果你开发的程序是基于Anroid3.0且计划使用search widget, 那么我们建议把search widget 作为一个an&lt;/span&gt;&lt;a rel="nofollow" href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionView" style="color:rgb(0,51,102); line-height:23px; text-decoration:none; font-size:13px"&gt;action view in the Action Bar&lt;/a&gt;&lt;span style="color:#003366; line-height:23px; font-size:13px"&gt;来使用, 而不是使用search dialog (也不要把search widget 放在你的activity layout中). 比如,&lt;/span&gt;&lt;span style="line-height:23px; font-size:13px"&gt;图二&lt;/span&gt;&lt;span style="color:#003366; line-height:23px; font-size:13px"&gt;就是把search widget 当做Action Bar来使用的一个界面.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="font-size:13px; color:#003366; line-height:23px"&gt;search widget提供了和search dialog一样的功能. 当用户执行搜索的时候，它会启动合适的activity来进行处理，它也提供搜索关键词建议和语言搜索.&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;注意&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#003366; line-height:28px; font-size:16px"&gt;:&lt;/span&gt;&lt;span style="font-size:13px; line-height:23px"&gt;&lt;span style="color:#003366; line-height:23px"&gt;当你把search widget作为action view来使用时, 你可能有时仍然需要使用search dialog, 比如有时&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;search widg&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;在&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;Action Bar中也不太合适. 具体的请参照下面的”&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; font-family:monospace; white-space:pre"&gt;&lt;span style="line-height:22px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:10px; line-height:25px"&gt;&lt;span style="line-height:18px"&gt;同时使用&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif"&gt;&lt;span style="font-size:10px; line-height:18px"&gt;&lt;span style="line-height:18px"&gt;search widget和search dialog&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:13px; line-height:23px"&gt;“&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;图二&lt;/span&gt;&lt;span style="font-size:16px; line-height:28px"&gt;：&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="line-height:25px; font-size:16px"&gt;&lt;span style="line-height:28px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;&lt;img alt="Searchable之五（使用Search Widget） - hubingforever - 民主与科学" src="http://developer.android.com/images/ui/actionbar-actionview.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"&gt;&lt;/span&gt;&lt;/div&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="line-height:25px"&gt;&lt;span style="line-height:25px; font-size:16px"&gt;&lt;span style="line-height:28px"&gt;&lt;br style="line-height:28px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="line-height:25px"&gt;十、配置&lt;/span&gt;&lt;strong&gt;search widget&lt;/strong&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;首先你应该向前面讲的&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; font-size:13px"&gt;search dialog&lt;/span&gt;&lt;span style="line-height:25px; font-size:13px"&gt;一样，先创建好searchable配置和searchable activity，然后为每个SearchView设置好搜索助手。你可以通过setSearchableInfo()来设置你的&lt;/span&gt;&lt;code style="line-height:25px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/app/SearchableInfo.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;&lt;span style="font-size:16px; line-height:23px"&gt;SearchableInfo&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;span style="font-size:13px; line-height:23px"&gt;对象，SearchableInfo它代表的是你的searchable配置。&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;你可以同过&lt;/span&gt;&lt;code style="line-height:22px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/app/SearchManager.html" style="color:rgb(207,121,28); line-height:22px; text-decoration:none"&gt;&lt;span style="font-size:16px; line-height:23px"&gt;SearchManage&lt;/span&gt;&lt;span style="font-size:13px; line-height:21px"&gt;r&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;span style="font-size:13px; line-height:23px"&gt;的&lt;/span&gt;&lt;span style="font-size:16px; line-height:28px"&gt;&lt;code style="line-height:22px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/app/SearchManager.html#getSearchableInfo%28android.content.ComponentName%29" style="color:rgb(207,121,28); line-height:22px; text-decoration:none"&gt;getSearchableInfo()&lt;/a&gt;&lt;/code&gt;&lt;/span&gt;&lt;span style="font-size:13px; line-height:23px"&gt;来得到一个SearchableInfo的引用。&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;比如, 如果你想在&lt;/span&gt;&lt;a rel="nofollow" href="http://developer.android.com/guide/topics/ui/actionbar.html" style="color:rgb(207,121,28); line-height:22px; text-decoration:none; font-size:13px"&gt;Action Bar&lt;/a&gt;&lt;span style="font-size:13px; line-height:23px"&gt;中把&lt;/span&gt;&lt;span style="font-size:16px; line-height:28px"&gt;&lt;code style="line-height:23px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/widget/SearchView.html" style="color:rgb(207,121,28); line-height:23px; text-decoration:none"&gt;SearchView&lt;/a&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:13px; color:#003366; line-height:23px"&gt;作为一个action view来使用,那么在 回调数&lt;code style="line-height:21px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29" style="color:rgb(207,121,28); line-height:21px; text-decoration:none"&gt;onCreateOptionsMenu()&lt;/a&gt;&lt;/code&gt;中就应该enable the widget&lt;/span&gt;&lt;span style="line-height:25px"&gt;实例10：&lt;/span&gt;&lt;/p&gt; &lt;p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"&gt; &lt;/p&gt; &lt;pre style="line-height:25px; font-weight:bold"&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;@Override&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;public&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;boolean&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; onCreateOptionsMenu&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;(&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;Menu&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; menu&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;)&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;{&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;// Inflate the options menu from XML&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;MenuInflater&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; inflater &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;=&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; getMenuInflater&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;();&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt; inflater&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;.&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;inflate&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;(&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;R&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;.&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;menu&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;.&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;options\_menu&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;,&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; menu&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;);&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;// Get the SearchView and set the searchable configuration&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="color:#ff9900; line-height:25px"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#ff9900; line-height:25px"&gt;&lt;span style="line-height:25px"&gt;SearchManager&lt;/span&gt;&lt;span style="line-height:25px"&gt; searchManager &lt;/span&gt;&lt;span style="line-height:25px"&gt;=&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;SearchManager&lt;/span&gt;&lt;span style="line-height:25px"&gt;)&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;code style="line-height:25px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#getSystemService%28java.lang.String%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;&lt;span style="line-height:25px"&gt;getSystemService&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;Context&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;SEARCH\_SERVICE&lt;/span&gt;&lt;span style="line-height:25px"&gt;);&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px"&gt;SearchView&lt;/span&gt;&lt;span style="line-height:25px"&gt; searchView &lt;/span&gt;&lt;span style="line-height:25px"&gt;=&lt;/span&gt;&lt;span style="line-height:25px"&gt; &lt;/span&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;SearchView&lt;/span&gt;&lt;span style="line-height:25px"&gt;)&lt;/span&gt;&lt;span style="line-height:25px"&gt; menu&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;findItem&lt;/span&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;R&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;id&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;menu\_search&lt;/span&gt;&lt;span style="line-height:25px"&gt;).&lt;/span&gt;&lt;span style="line-height:25px"&gt;getActionView&lt;/span&gt;&lt;span style="line-height:25px"&gt;();&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;br style="line-height:25px"&gt; searchView&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;setSearchableInfo&lt;/span&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;searchManager&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;getSearchableInfo&lt;/span&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;code style="line-height:25px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/app/Activity.html#getComponentName%28%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;&lt;span style="line-height:25px"&gt;getComponentName&lt;/span&gt;&lt;span style="line-height:25px"&gt;()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;span style="line-height:25px"&gt;));&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;br style="line-height:25px"&gt; searchView&lt;/span&gt;&lt;span style="line-height:25px"&gt;.&lt;/span&gt;&lt;span style="line-height:25px"&gt;setIconifiedByDefault&lt;/span&gt;&lt;span style="line-height:25px"&gt;(&lt;/span&gt;&lt;span style="line-height:25px"&gt;false&lt;/span&gt;&lt;span style="line-height:25px"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;// Do not iconify the widget; expand it by default、&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;return&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;true&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;&lt;br style="line-height:25px"&gt;&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;}&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="font-size:16px; color:#000080; line-height:23px"&gt;这样就完了，search widget现在已经被配置好了，系统也能够把搜索命令发送到你的searchable activity. 你也可以在search widge&lt;span style="line-height:22px"&gt;t&lt;/span&gt;&lt;span style="line-height:22px"&gt;中使用&lt;/span&gt;&lt;span style="line-height:22px"&gt;search suggestions。&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="font-size:16px; line-height:23px"&gt;&lt;span style="line-height:23px"&gt;注意:&lt;/span&gt;&lt;span style="color:#000080; line-height:23px"&gt;&lt;span style="line-height:23px"&gt; &lt;/span&gt;如果你想自己处理所有用户的输入，请在它的回调函数和事件监听接口方法中处理。关于SearchView 和其监听接口，更多的内容可以参照&lt;span style="line-height:23px; white-space:normal"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/widget/SearchView.html" style="color:rgb(207,121,28); line-height:23px; text-decoration:none"&gt;SearchView&lt;/a&gt;的文档。&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="font-size:16px; line-height:23px"&gt;&lt;span style="color:#000080; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:normal"&gt;关于&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:23px; color:rgb(0,0,128); white-space:normal"&gt;Action Bar中&lt;/span&gt;&lt;span style="line-height:23px; color:rgb(0,0,128); white-space:normal"&gt;action views的更多内容，请参照&lt;/span&gt;&lt;span style="line-height:23px; color:rgb(0,0,128); white-space:normal"&gt;&lt;a rel="nofollow" href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionView" style="color:rgb(207,121,28); line-height:23px; text-decoration:none"&gt;Using the Action Bar&lt;/a&gt;(里面有把search widget作为action view来进行添加的实例代码）&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;pre style="line-height:25px"&gt;&lt;span style="line-height:25px; white-space:normal"&gt;&lt;span style="line-height:25px"&gt;十一、search widget的其他特性&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="line-height:25px"&gt;&lt;span style="font-size:16px; color:#000080; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:normal"&gt;可以向SearchView 添加一些其他的特性&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt; &lt;pre style="line-height:25px"&gt;&lt;span style="line-height:25px; white-space:normal"&gt;&lt;span style="font-size:13px; line-height:21px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt; &lt;dl style="line-height:25px"&gt; &lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;&lt;span style="line-height:20px; white-space:normal"&gt;&lt;span style="line-height:23px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;&lt;span style="line-height:20px; white-space:normal"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px; font-weight:bold"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;A、提交按钮（A submit button）&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; line-height:25px"&gt;&lt;span style="font-size:13px; color:#000080; line-height:23px"&gt;默认情况下是没提交搜索的按钮，所以用户必须在键盘上按下"Return"键来提交搜索.你可以同过setSubmitButtonEnabled(true)来添加一个提交按钮（"submit" button)&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt; &lt;span style="font-family:Arial,Helvetica,sans-serif; line-height:25px"&gt;&lt;span style="font-size:13px; color:#000080; line-height:23px"&gt;注：这里 的&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,0,128); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;"Return"键应该就是”Enter“&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,0,128); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;键&lt;/span&gt; &lt;/dt&gt; &lt;dt style="line-height:25px; font-weight:bold"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;B、自定义search suggestions&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; line-height:25px"&gt;&lt;span style="font-size:13px; color:#000080; line-height:23px"&gt;当你使用search suggestions的时候, 你经常希望用户仅仅是简单选择suggestion, 但是他们也可能想自定义suggested search query. 你可以通过调用setQueryRefinementEnabled(true)，来为每个suggestion添加一个按钮，让用户在search box中输入用户自定义的suggestion&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px; font-weight:bold"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;C、让 search box 可以见&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; line-height:25px"&gt;&lt;span style="font-size:13px; color:#000080; line-height:23px"&gt;默认情况下, search widget是"iconified“的，只是用一个图标 来表示它(一个放大镜), 当用户按下它的时候才显示search box . 你可以调用setIconifiedByDefault(false)让search box默认都被显示。 你也可以调用setIconified()让它以iconified“的形式显示。&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; line-height:25px"&gt;&lt;span style="font-size:13px; color:#000080; line-height:23px"&gt;&lt;br style="line-height:23px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;&lt;span style="color:#003366; line-height:23px"&gt;在&lt;code style="line-height:21px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/reference/android/widget/SearchView.html" style="color:rgb(207,121,28); line-height:21px; text-decoration:none"&gt;SearchView&lt;/a&gt;&lt;/code&gt; 中还有其他的一些API允许你个性化search widget的显示.然而他们大多数是在你自己处理用户输入而不&lt;/span&gt;&lt;span style="line-height:23px; color:rgb(0,51,102)"&gt;Android system处理输入和显示search suggestions时使用&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:23px"&gt;&lt;span style="line-height:23px; color:rgb(0,51,102)"&gt;&lt;br style="line-height:23px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;&lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:16px; line-height:28px"&gt;十二、同时使用&lt;/span&gt;&lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; font-size:16px"&gt;search widget和search dialog&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt; &lt;span style="line-height:25px; font-family:Arial,Helvetica,sans-serif"&gt;&lt;span style="font-size:13px; color:#003366; line-height:23px"&gt;如果你把&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;search widget作为&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;&lt;a rel="nofollow" href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionView" style="color:rgb(207,121,28); line-height:20px; text-decoration:none"&gt;action view&lt;/a&gt;而插入到&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;Action Bar中, 那么你可以让它只在有足够空间时才以&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;&lt;span style="font-size:13px; line-height:21px"&gt;&lt;span style="line-height:20px; font-family:Arial,Helvetica,sans-serif"&gt;Action Bar的形式出现(通过设置android:showAsAction="ifRoom"&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:13px; line-height:20px; font-family:Arial,Helvetica,sans-serif"&gt;), 这时search widget就可能不以&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;action view的形式出现, 而是是以菜单的形式出现在overflow menu里. 比如你的程序运行在一个小屏幕的手机上，在&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt; Action Bar中&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;就没有足够的空间把&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;search widget和其他的&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;action items or navigation elements显示下。这时它们将以菜单项的形式出现在&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;overflow menu中. 当在overflow menu中显示的时候,该项像普通的菜单项一样&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt; and 它不再显示 action view (the search widget).&lt;/span&gt; &lt;/dt&gt; &lt;dt style="line-height:25px"&gt; &lt;span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; color:#003366; line-height:23px"&gt;&lt;span style="line-height:20px"&gt;为了处理该情况,当用户选择了和&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;search widget关联的&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;menu item时，你必须激活search dialog。为了处理此事情, 你必须在onOptionsItemSelected() h处理该"Search" menu item并通过调用&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;onSearchRequested()来开启&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;search dialog。&lt;/span&gt; &lt;/dt&gt; &lt;dt style="line-height:25px"&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;&lt;br style="line-height:23px"&gt;&lt;/span&gt;&lt;/dt&gt; &lt;dt style="line-height:25px"&gt; &lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;关于&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;Action Bar如何工作和处理此中情况的更多信息请参照文档&lt;/span&gt;&lt;a rel="nofollow" href="http://developer.android.com/guide/topics/ui/actionbar.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;Using the Action&lt;/a&gt;&lt;a rel="nofollow" href="http://developer.android.com/guide/topics/ui/actionbar.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;Bar&lt;/a&gt;。&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;也可以参照&lt;/span&gt;&lt;span style="line-height:20px; color:rgb(0,51,102); font-family:Arial,Helvetica,sans-serif; font-size:13px"&gt;search dialog 和search widget的实例&lt;/span&gt;&lt;a rel="nofollow" href="http://developer.android.com/resources/samples/SearchableDictionary/src/com/example/android/searchabledict/SearchableDictionary.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;Searchable Dictionary&lt;/a&gt; &lt;/dt&gt; &lt;/dl&gt; &lt;/div&gt; &lt;/wbr&gt;