国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android ActionBar搜索功能用法詳解

2019-12-12 05:51:30
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 疏附县| 社会| 西青区| 察雅县| 桃园县| 天峻县| 永胜县| 台安县| 六枝特区| 格尔木市| 洪雅县| 东港市| 股票| 永登县| 和田县| 靖远县| 曲阳县| 宣化县| 三亚市| 奇台县| 绵阳市| 奉化市| 金塔县| 铜山县| 赤水市| 怀仁县| 道真| 呼和浩特市| 乐清市| 顺昌县| 会同县| 启东市| 永济市| 富阳市| 平谷区| 镇赉县| 长武县| 乌兰察布市| 曲阳县| 鹤壁市| 福贡县|