android 系统搜索框(有浏览记录)

先看下效果:



   一、配置搜索描述文件

 要在res中的xml文件加创建sreachable.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<searchable
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:hint="@string/searchLable" 
  android:label="@string/searchLable"
  android:searchSuggestAuthority="com.glacier.ui.SearchSuggestionProvider"
  android:searchSuggestSelection=" ? ">
  
</searchable>
  二、填写配置文件信息


1.搜索框的配置

<!-- 搜索动作 -->
            <intent-filter >
                <action android:name="android.intent.action.SEARCH" >
                </action>
            </intent-filter>

            <meta-data
                android:name="android.app.default_searchable"
                android:value="MainActivity" />
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" >
            </meta-data>

2.保存内容的配置

<provider
            android:authorities="com.glacier.ui.SearchSuggestionProvider"
            android:name="com.glacier.ui.SearchSuggestionProvider" >
        </provider>

三、调用启动搜索框方法

//弹出搜索框
onSearchRequested();

可以重新写系统的方法做些必要的内容加载其他

      

 @Override
	public boolean onSearchRequested(){
		//打开浮动搜索框(第一个参数默认添加到搜索框的值)      
		startSearch(null, false, null, false);
		return true;
	}
    
  //得到搜索结果
  	@Override
  	public void onNewIntent(Intent intent){
  		super.onNewIntent(intent);
  		//获得搜索框里值
  		query=intent.getStringExtra(SearchManager.QUERY);
  		System.out.println(query);
  		//保存搜索记录
  		SearchRecentSuggestions suggestions=new SearchRecentSuggestions(MainActivity.this,
  				SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
  		suggestions.saveRecentQuery(query, null);
  		System.out.println("保存成功");
  	}

四、记得要写存储的地方

import android.content.SearchRecentSuggestionsProvider;

public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {

	public final static String AUTHORITY="com.glacier.ui.SearchSuggestionProvider";
	public final static int MODE=DATABASE_MODE_QUERIES;
	
	public SearchSuggestionProvider(){
		super();
		setupSuggestions(AUTHORITY, MODE);
	}
}


欢迎各位开发人员探讨知识!!!

源码下载地址http://download.csdn.net/detail/wang_yubin/4472156


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值