调用系统内部 searchUI的基本用法

/* 按下键盘即调用搜索框 */ 
        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); 
    onSearchRequested();/这里没有重写onSearchRequested()方法,只是单单启动系统search UI 

当按下搜索按钮,系统就会自动发送Intent,action是Intent.ACTION_SEARCH,可以通过 
intent.getStringExtra(SearchManager.QUERY);和intent 
                    .getBundleExtra(SearchManager.APP_DATA);获取数据,具体看下面的Intent参数的传递与获取 

在AndroidManifest.xml文件加入 
Java代码
<activity android:name=".SearchActivity">
                        <intent-filter>
                                <action android:name="android.intent.action.SEARCH" />
                                <category android:name="android.intent.category.DEFAULT" />
                        </intent-filter>
                        <meta-data
                        android:name="android.app.searchable"
                        android:resource="@xml/searchable"/>
            </activity>        Java代码
Intent queryIntent = getIntent();
             String queryAction = queryIntent.getAction();
             /* 取得当按下搜索时的Intent */
             if (Intent.ACTION_SEARCH.equals(queryAction))
             {
                     /* 取得所要搜索的字符串 */
                     String str = queryIntent.getStringExtra(SearchManager.QUERY);
                     Log.i("msg", str);
                     query(str);
}
searchable.xml文件 
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/search" 
        android:hint="@string/search_hint" 
        android:searchSuggestAuthority="com.mcs.todo"
/>
还可以通过类SearchRecentSuggestions来保存搜索记录和清除搜索记录,具体看API demo 

Intent参数的传递与获取,下面是转载的 
在android利用数据库实现搜索联想功能一文中主要介绍了数据的联想和联想列表的显示,但是没有设计到点击搜索按钮时,activty的跳转和参数的传递功能。下面主要介绍一下activty的调转和参数的传递和获取。在实际的应用中经常用到搜索功能,当用户搜索完毕以后,可能会跳到另外一个activty,并且需要或得之前的activty的一些参数。下面先看一下效果图: 


其中第三幅图中的最后一行的内容是第一幅图的activty中传递的参数。 

实现的主要代码是: 

传递参数的activty: 
Java代码 
重写onSearchRequested()方法来设置携带的数据
@Override
    public boolean onSearchRequested() {
        Bundle appDataBundle = new Bundle();
        appDataBundle.putString("welcome", "wangjun");
        startSearch("搜索", true, appDataBundle, false);
        return true;
    }得到参数的activty: 
Java代码
TextView textView=(TextView)findViewById(R.id.text1);
        TextView textView1=(TextView)findViewById(R.id.text2);
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String queryString = intent.getStringExtra(SearchManager.QUERY);
            textView.setText("需要搜索的内容是:"+queryString);
            final Bundle appData = intent
                    .getBundleExtra(SearchManager.APP_DATA);
            if (appData != null) {
                Log.i("welcome", "appDate is not null");
                textView1.setText("之前activty传递的参数是:"+appData.getString("welcome"));
            } else {
                Log.i("welcome", "appDate is  null");
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值