最近看android 4.0 mms,contact源碼,發現其中搜尋都是searchview控件,下面自己試着寫一個
效果

1.在res/menu/檔案夾下建立menu.xml 配置檔案
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search" android:title="@string/search"
android:icon="@drawable/search_app_icon"
android:showasaction="ifroom|withtext" //在actionbar顯示,
android:actionviewclass="android.widget.searchview"
></item>
</menu>
2.建立一個activity,并為該activity添加菜單項,createoptionsmenu代碼
@override
public boolean oncreateoptionsmenu(menu menu) {
// todo auto-generated method stub
mi.inflate(r.menu.my_menu, menu);
menuitem search=menu.finditem(r.id.search);
search.collapseactionview();
searchview searchview=(searchview) search.getactionview();
searchview.seticonifiedbydefault(false);
searchmanager msearchmanager=(searchmanager)getsystemservice(context.search_service);
searchableinfo info=msearchmanager.getsearchableinfo(getcomponentname());
searchview.setsearchableinfo(info); //需要在xml檔案加下建立searchable.xml,搜尋框配置檔案
return true;
}
3.在xml檔案夾下履歷searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_title"
android:searchsettingsdescription="@string/search_title"
android:searchsuggestauthority="com.digua.fncontact.familyname"
android:searchsuggestintentaction="android.intent.action.view"
android:searchsuggestintentdata="content://com.digua.fncontact.familyname/fname"
android:searchsuggestselection=" ?"
android:searchsuggestthreshold="1"
android:inputtype="text"
android:voicesearchmode="showvoicesearchbutton|launchrecognizer"
android:searchmode="queryrewritefromtext"
android:includeinglobalsearch="true"
android:queryafterzeroresults="true"
>
</searchable>
具體serachable屬性請看上一篇轉載部落格,
http://blog.csdn.net/androidchuxueze/article/details/7590732
示例下載下傳
http://download.csdn.net/detail/androidchuxueze/5005369