Android搜索控件SearchView的用法

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值