萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android 系統有浏覽記錄搜索框

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); 

}

copyright © 萬盛學電腦網 all rights reserved