閱讀552 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android搜索控件SearchView的用法

最近看Android 4.0  mms,contact源碼,發現其中搜索都是SearchView控件,下麵自己試著寫一個

效果

1.在res/menu/文件夾下建立menu.xml 配置文件

<menu
  xmlns:andro_blank" href="https://schemas.android.com/apk/res/android" >https://schemas.android.com/apk/res/android">
    <item android: 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:andro_blank" href="https://schemas.android.com/apk/res/android" >https://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屬性請看上一篇轉載博客,

https://blog.csdn.net/androidchuxueze/article/details/7590732

示例下載

https://download.csdn.net/detail/androidchuxueze/5005369

最後更新:2017-04-03 12:54:04

  上一篇:go sed-加速你在Linux的文件編輯
  下一篇:go 數據挖掘apriori算法Java代碼實現