Android ActionBar搜尋功能用法詳解_Android

來源:互聯網
上載者:User

本文執行個體講述了Android ActionBar搜尋功能用法。分享給大家供大家參考,具體如下:

使用ActionBar SearchView時的注意點:

首先要吐槽一下Android的官方Guide文檔 ,關於用法講得不明確,可能是一直沒更新的原因吧。

本來照著文檔搞了一下,hint死活出不來,也無法跳轉到搜尋結果Activity。

StackOverflow也有人提出了這個問題,答案說得很明白 - 參考連結。

正確用法

1. 在AndroidManifest.xml中為提供SearchView的Activity添加meta-data

<activity  android:name=".navigation.NavigationActivity"  android:label="@string/title_activity_navigation">  <intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.DEFAULT" />  </intent-filter>  <meta-data    android:name="android.app.default_searchable"    android:value=".search.SearchResultActivity" /></activity>

2. 在提供搜尋結果的Activity中添加為SearchableInfo用的meta-data

<activity  android:name=".search.SearchResultActivity"  android:label="@string/title_activity_search_result"  android:launchMode="singleTop">  <intent-filter>    <action android:name="android.intent.action.SEARCH" />  </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>

3. @xml/searchable檔案中的Android:hint只能使用string.xml中定義的字串,不能hard-coded

<?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"></searchable>

4. 初始化Menu的時候,擷取SearchableInfo

SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

注意點

SearchManager通過ComponentName尋找SearchableInfo的時候,對應Component必須滿足一定條件:

1. intent-filter包含

<action android:name="android.intent.action.SEARCH" />

2. meta-data

<meta-data  android:name="android.app.searchable"  android:resource="@xml/searchable" />

另一種方法

既然SearchManager是通過ComponentName來擷取SearchableInfo,當然可以直接從提供搜尋結果的Activity中擷取ComponentName。

SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();ComponentName cn = new ComponentName("com.liangfeizc.catykanji", "com.liangfeizc.catykanji.search.SearchResultActivity");searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));

tips

ComponentName建構函式的第一個參數pkg是Application的Package,不是目標類所在的Package。

The first parameter ComponentName(String pkg, String cls) is application package not the package where the activity is.

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android操作XML資料技巧總結》、《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》、《Android檔案操作技巧匯總》、《Android操作SQLite資料庫技巧總結》、《Android操作json格式資料技巧總結》、《Android資料庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》及《Android控制項用法總結》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.