編輯:關於Android編程
本文實例講述了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程序設計有所幫助。
Android的Activity跳轉動畫各種效果整理
大家使用Android的原生UI都知道,Android的Activity跳轉就是很生硬的切換界面。其實Android的Activity跳轉可以設置各種動畫。下面給大家看看
Android多種樣式的進度條
---- The mark of the immature man is that he wants to die nobly for a causer wh
微信怎麼投訴微商賣家 微信怎麼投訴好友
有時候,我們的微信會收到一些不認識的人發的廣告,甚至是一些微商的詐騙信息,特別一些微信群上,時不時就會遇到一些做垃圾廣告或者不法分子詐騙的,或者不小心就被加
Dex文件格式詳解
什麼是dex文件他是Android系統的可執行文件,包含應用程序的全部操作指令以及運行時數據。由於dalvik是一種針對嵌入式設備而特殊設計的java虛擬機,所以dex文