最近看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