最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ActionBar搜索功能用法詳解

 更新時間:2016年07月27日 17:35:15   作者:feelang  
這篇文章主要介紹了Android ActionBar搜索功能用法,較為詳細的分析了Android ActionBar搜索功能使用中的相關注意事項與操作技巧,需要的朋友可以參考下

本文實例講述了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構造函數(shù)的第一個參數(shù)pkg是Application的Package,不是目標類所在的Package。

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

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android操作XML數(shù)據(jù)技巧總結》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》及《Android控件用法總結

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

相關文章

最新評論

左云县| 突泉县| 嵩明县| 陵川县| 闻喜县| 泉州市| 搜索| 双牌县| 克什克腾旗| 井研县| 新郑市| 南澳县| 商南县| 漠河县| 藁城市| 大同市| 海淀区| 江城| 太湖县| 宜都市| 原阳县| 师宗县| 大理市| 深州市| 芒康县| 隆回县| 利川市| 岱山县| 海盐县| 东宁县| 田林县| 乾安县| 阿瓦提县| 霍州市| 兴海县| 万山特区| 藁城市| 来安县| 师宗县| 湘西| 泾川县|