> 編寫:[Lin-H](https://github.com/Lin-H) - 原文:[http://developer.android.com/training/search/setup.html](http://developer.android.com/training/search/setup.html)
從Android 3.0開始,在action bar中使用[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)作為item,是在你的app中提供搜索的一種更好方法。像其他所有在action bar中的item一樣,你可以定義[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)在有足夠空間的時候總是顯示,或設置為一個折疊操作(collapsible action),一開始[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)作為一個圖標顯示,當用戶點擊圖標時再顯示搜索框占據整個action bar。
> **Note**:在本課程的后面,你會學習對那些不支持[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)的設備,如何使你的app向下兼容至Android 2.1(API level 7)版本。
### 添加Search View到action bar中
為了在action bar中添加[SearchView](http://developer.android.com/reference/android/widget/SearchView.html),在你的工程目錄`res/menu/`中創建一個名為`options_menu.xml`的文件,再把下列代碼添加到文件中。這段代碼定義了如何創建search item,比如使用的圖標和item的標題。`collapseActionView`屬性允許你的[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)占據整個action bar,在不使用的時候折疊成普通的action bar item。由于在手持設備中action bar的空間有限,建議使用`collapsibleActionView`屬性來提供更好的用戶體驗。
~~~
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
~~~
> **Note**:如果你的menu items已經有一個XML文件,你可以只把`<item>`元素添加入文件。
要在action bar中顯示[SearchView](http://developer.android.com/reference/android/widget/SearchView.html),在你的[activity](# "An activity represents a single screen with a user interface.")中[onCreateOptionsMenu()](http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu))方法內填充XML菜單資源(`res/menu/options_menu.xml`):
~~~
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
~~~
如果你立即運行你的app,[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)就會顯示在你app的action bar中,但還無法使用。你現在需要定義[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)如何運行。
### 創建一個檢索配置
[檢索配置(searchable configuration)](http://developer.android.com/guide/topics/search/searchable-config.html)在 `res/xml/searchable.xml`文件中定義了[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)如何運行。檢索配置中至少要包含一個`android:label`屬性,與Android manifest中的`<application>`或`<activity>``android:label`屬性值相同。但我們還是建議添加`android:hint`屬性來告訴用戶應該在搜索框中輸入什么內容:
~~~
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />
~~~
在你的應用的manifest文件中,聲明一個指向`res/xml/searchable.xml`文件的[`<meta-data>`](http://developer.android.com/guide/topics/manifest/meta-data-element.html)元素,來告訴你的應用在哪里能找到檢索配置。在你想要顯示[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)的`<activity>`中聲明`<meta-data>`元素:
~~~
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
~~~
在你之前創建的[onCreateOptionsMenu()](http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu))方法中,調用[setSearchableInfo(SearchableInfo)](http://developer.android.com/reference/android/widget/SearchView.html#setSearchableInfo(android.app.SearchableInfo))把[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)和檢索配置關聯在一起:
~~~
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// 關聯檢索配置和SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
~~~
調用[getSearchableInfo()](http://developer.android.com/reference/android/app/SearchManager.html#getSearchableInfo(android.content.ComponentName))返回一個[SearchableInfo](http://developer.android.com/reference/android/app/SearchableInfo.html)由檢索配置XML文件創建的對象。檢索配置與[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)正確關聯后,當用戶提交一個搜索請求時,[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)會以[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH) intent啟動一個[activity](# "An activity represents a single screen with a user interface.")。所以你現在需要一個能過濾這個intent和處理搜索請求的[activity](# "An activity represents a single screen with a user interface.")。
### 創建一個檢索[activity](# "An activity represents a single screen with a user interface.")
當用戶提交一個搜索請求時,[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)會嘗試以[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH)啟動一個[activity](# "An activity represents a single screen with a user interface.")。檢索[activity](# "An activity represents a single screen with a user interface.")會過濾[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH) intent并在某種數據集中根據請求進行搜索。要創建一個檢索[activity](# "An activity represents a single screen with a user interface."),在你選擇的[activity](# "An activity represents a single screen with a user interface.")中聲明對[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH) intent過濾:
~~~
<activity android:name=".SearchResultsActivity" ... >
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
~~~
在你的檢索[activity](# "An activity represents a single screen with a user interface.")中,通過在[onCreate()](http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle))方法中檢查[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH) intent來處理它。
> **Note**:如果你的檢索[activity](# "An activity represents a single screen with a user interface.")在single top mode下啟動(`android:launchMode="singleTop"`),也要在[onNewIntent()](http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent))方法中處理[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH) intent。在single top mode下你的[activity](# "An activity represents a single screen with a user interface.")只有一個會被創建,而隨后啟動的[activity](# "An activity represents a single screen with a user interface.")將不會在棧中創建新的[activity](# "An activity represents a single screen with a user interface.")。這種啟動模式很有用,因為用戶可以在當前[activity](# "An activity represents a single screen with a user interface.")中進行搜索,而不用在每次搜索時都創建一個[activity](# "An activity represents a single screen with a user interface.")實例。
~~~
public class SearchResultsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
...
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
...
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//通過某種方法,根據請求檢索你的數據
}
}
...
}
~~~
如果你現在運行你的app,[SearchView](http://developer.android.com/reference/android/widget/SearchView.html)就能接收用戶的搜索請求,以[ACTION_SEARCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEARCH) intent啟動你的檢索[activity](# "An activity represents a single screen with a user interface.")。現在就由你來解決如何依據請求來儲存和搜索數據。
- 序言
- Android入門基礎:從這里開始
- 建立第一個App
- 創建Android項目
- 執行Android程序
- 建立簡單的用戶界面
- 啟動其他的Activity
- 添加ActionBar
- 建立ActionBar
- 添加Action按鈕
- 自定義ActionBar的風格
- ActionBar的覆蓋層疊
- 兼容不同的設備
- 適配不同的語言
- 適配不同的屏幕
- 適配不同的系統版本
- 管理Activity的生命周期
- 啟動與銷毀Activity
- 暫停與恢復Activity
- 停止與重啟Activity
- 重新創建Activity
- 使用Fragment建立動態的UI
- 創建一個Fragment
- 建立靈活動態的UI
- Fragments之間的交互
- 數據保存
- 保存到Preference
- 保存到文件
- 保存到數據庫
- 與其他應用的交互
- Intent的發送
- 接收Activity返回的結果
- Intent過濾
- Android分享操作
- 分享簡單的數據
- 給其他App發送簡單的數據
- 接收從其他App返回的數據
- 給ActionBar增加分享功能
- 分享文件
- 建立文件分享
- 分享文件
- 請求分享一個文件
- 獲取文件信息
- 使用NFC分享文件
- 發送文件給其他設備
- 接收其他設備的文件
- Android多媒體
- 管理音頻播放
- 控制音量與音頻播放
- 管理音頻焦點
- 兼容音頻輸出設備
- 拍照
- 簡單的拍照
- 簡單的錄像
- 控制相機硬件
- 打印
- 打印照片
- 打印HTML文檔
- 打印自定義文檔
- Android圖像與動畫
- 高效顯示Bitmap
- 高效加載大圖
- 非UI線程處理Bitmap
- 緩存Bitmap
- 管理Bitmap的內存
- 在UI上顯示Bitmap
- 使用OpenGL ES顯示圖像
- 建立OpenGL ES的環境
- 定義Shapes
- 繪制Shapes
- 運用投影與相機視圖
- 添加移動
- 響應觸摸事件
- 添加動畫
- View間漸變
- 使用ViewPager實現屏幕側滑
- 展示卡片翻轉動畫
- 縮放View
- 布局變更動畫
- Android網絡連接與云服務
- 無線連接設備
- 使得網絡服務可發現
- 使用WiFi建立P2P連接
- 使用WiFi P2P服務
- 執行網絡操作
- 連接到網絡
- 管理網絡
- 解析XML數據
- 高效下載
- 為網絡訪問更加高效而優化下載
- 最小化更新操作的影響
- 避免下載多余的數據
- 根據網絡類型改變下載模式
- 云同步
- 使用備份API
- 使用Google Cloud Messaging
- 解決云同步的保存沖突
- 使用Sync Adapter傳輸數據
- 創建Stub授權器
- 創建Stub Content Provider
- 創建Sync Adpater
- 執行Sync Adpater
- 使用Volley執行網絡數據傳輸
- 發送簡單的網絡請求
- 建立請求隊列
- 創建標準的網絡請求
- 實現自定義的網絡請求
- Android聯系人與位置信息
- Android聯系人信息
- 獲取聯系人列表
- 獲取聯系人詳情
- 使用Intents修改聯系人信息
- 顯示聯系人頭像
- Android位置信息
- 獲取最后可知位置
- 獲取位置更新
- 顯示位置地址
- 創建和監視地理圍欄
- Android可穿戴應用
- 賦予Notification可穿戴特性
- 創建Notification
- 在Notifcation中接收語音輸入
- 為Notification添加顯示頁面
- 以Stack的方式顯示Notifications
- 創建可穿戴的應用
- 創建并運行可穿戴應用
- 創建自定義的布局
- 添加語音功能
- 打包可穿戴應用
- 通過藍牙進行調試
- 創建自定義的UI
- 定義Layouts
- 創建Cards
- 創建Lists
- 創建2D-Picker
- 創建確認界面
- 退出全屏的Activity
- 發送并同步數據
- 訪問可穿戴數據層
- 同步數據單元
- 傳輸資源
- 發送與接收消息
- 處理數據層的事件
- Android TV應用
- 創建TV應用
- 創建TV應用的第一步
- 處理TV硬件部分
- 創建TV的布局文件
- 創建TV的導航欄
- 創建TV播放應用
- 創建目錄瀏覽器
- 提供一個Card視圖
- 創建詳情頁
- 顯示正在播放卡片
- 幫助用戶在TV上探索內容
- TV上的推薦內容
- 使得TV App能夠被搜索
- 使用TV應用進行搜索
- 創建TV游戲應用
- 創建TV直播應用
- TV Apps Checklist
- Android企業級應用
- Ensuring Compatibility with Managed Profiles
- Implementing App Restrictions
- Building a Work Policy Controller
- Android交互設計
- 設計高效的導航
- 規劃屏幕界面與他們之間的關系
- 為多種大小的屏幕進行規劃
- 提供向下和橫向導航
- 提供向上和歷史導航
- 綜合:設計樣例 App
- 實現高效的導航
- 使用Tabs創建Swipe視圖
- 創建抽屜導航
- 提供向上的導航
- 提供向后的導航
- 實現向下的導航
- 通知提示用戶
- 建立Notification
- 當啟動Activity時保留導航
- 更新Notification
- 使用BigView風格
- 顯示Notification進度
- 增加搜索功能
- 建立搜索界面
- 保存并搜索數據
- 保持向下兼容
- 使得你的App內容可被Google搜索
- 為App內容開啟深度鏈接
- 為索引指定App內容
- Android界面設計
- 為多屏幕設計
- 兼容不同的屏幕大小
- 兼容不同的屏幕密度
- 實現可適應的UI
- 創建自定義View
- 創建自定義的View類
- 實現自定義View的繪制
- 使得View可交互
- 優化自定義View
- 創建向后兼容的UI
- 抽象新的APIs
- 代理至新的APIs
- 使用舊的APIs實現新API的效果
- 使用版本敏感的組件
- 實現輔助功能
- 開發輔助程序
- 開發輔助服務
- 管理系統UI
- 淡化系統Bar
- 隱藏系統Bar
- 隱藏導航Bar
- 全屏沉浸式應用
- 響應UI可見性的變化
- 創建使用Material Design的應用
- 開始使用Material Design
- 使用Material的主題
- 創建Lists與Cards
- 定義Shadows與Clipping視圖
- 使用Drawables
- 自定義動畫
- 維護兼容性
- Android用戶輸入
- 使用觸摸手勢
- 檢測常用的手勢
- 跟蹤手勢移動
- Scroll手勢動畫
- 處理多觸摸手勢
- 拖拽與縮放
- 管理ViewGroup中的觸摸事件
- 處理鍵盤輸入
- 指定輸入法類型
- 處理輸入法可見性
- 兼容鍵盤導航
- 處理按鍵動作
- 兼容游戲控制器
- 處理控制器輸入動作
- 支持不同的Android系統版本
- 支持多個控制器
- Android后臺任務
- 在IntentService中執行后臺任務
- 創建IntentService
- 發送工作任務到IntentService
- 報告后臺任務執行狀態
- 使用CursorLoader在后臺加載數據
- 使用CursorLoader執行查詢任務
- 處理查詢的結果
- 管理設備的喚醒狀態
- 保持設備的喚醒
- 制定重復定時的任務
- Android性能優化
- 管理應用的內存
- 代碼性能優化建議
- 提升Layout的性能
- 優化layout的層級
- 使用include標簽重用layouts
- 按需加載視圖
- 使得ListView滑動順暢
- 優化電池壽命
- 監測電量與充電狀態
- 判斷與監測Docking狀態
- 判斷與監測網絡連接狀態
- 根據需要操作Broadcast接受者
- 多線程操作
- 在一個線程中執行一段特定的代碼
- 為多線程創建線程池
- 啟動與停止線程池中的線程
- 與UI線程通信
- 避免出現程序無響應ANR
- JNI使用指南
- 優化多核處理器(SMP)下的Android程序
- Android安全與隱私
- Security Tips
- 使用HTTPS與SSL
- 為防止SSL漏洞而更新Security
- 使用設備管理條例增強安全性
- Android測試程序
- 測試你的Activity
- 建立測試環境
- 創建與執行測試用例
- 測試UI組件
- 創建單元測試
- 創建功能測試
- 術語表