原文地址:Android 浮动搜索框的使用作者:小强
当你需要在应用程序中提供搜索服务时,你第一个想到的是搜索框要放哪呢?其实Android已经为我们提供了一个浮动搜索框。Android手机上有一个专门的搜索按钮,用于处理搜索服务(当然有的定制版阉割了此按钮)。下面通过一个简单的搜索示例,展示Android浮动搜索框的用法。
首先展示一下我demo示例的项目结构,如下图:
?
其中?
- MainActivity.java 是应用程序的第一个Activity
- SearchResultActivity.java 是搜索结果Activity
- SearchSuggestionSampleProvider.java 是搜索建议提供类
- searchable.xml是搜索框的布局文件
1)首先在res目录下建立xml/searchable.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchMode="showSearchLabelAsBadge"
<!-- 搜索建议提供类-->
android:searchSuggestAuthority="org.freedom.search.SearchSuggestionSampleProvider"
android:searchSuggestSelection=" ? "
>
</searchable>?
2)strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ? <string name="hello">搜索浮动框</string>
? ? <string name="app_name">搜索浮动框示例</string>
? ? <string name="search_label">搜索</string>
? ? <string name="search_hint">请输入搜索关键字</string>
</resources>
3) 应用主界面 MainActivity.java
package org.freedom.search;
import org.freedom.search.R;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
? ??
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ?? super.onCreate(savedInstanceState);
? ?? setContentView(R.layout.main);
? ? }
}
4)搜索结果界面 SearchResultActivity.java
package org.freedom.search;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.widget.TextView;
public class SearchResultActivity extends Activity {
private TextView result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = new TextView(this);
setContentView(result);
setTitle("搜索结果");
Intent intent = getIntent();
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String queryKeyword = intent.getStringExtra(SearchManager.QUERY);
saveRecentSearchHistory(queryKeyword);
doSearch(queryKeyword);
}
}
//保存最近搜索的关键字记录
private void saveRecentSearchHistory(String queryKeyword) {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(queryKeyword, null);
}
//清除最近搜索的关键字记录
private void clearRecentSearchHistory() {
SearchRecentSuggestions suggestions =new SearchRecentSuggestions(this,?
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}
private void doSearch(String queryKeyword) {
result.setText("Your search keyword is : " queryKeyword);
}
@Override
protected void onDestroy() {
clearRecentSearchHistory();
super.onDestroy();
}
}
5)搜索建议提供类?SearchSuggestionSampleProvider.java
package org.freedom.search;
import android.content.SearchRecentSuggestionsProvider;
public class SearchSuggestionSampleProvider extends SearchRecentSuggestionsProvider{
public final static String AUTHORITY = SearchSuggestionSampleProvider.class.getName();
public final static int MODE = DATABASE_MODE_QUERIES;
public SearchSuggestionSampleProvider() {
super();
setupSuggestions(AUTHORITY, MODE);
}
}
6)AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
? ? ? package="org.freedom.search"
? ? ? android:versionCode="1"
? ? ? android:versionName="1.0">
? ? <application android:icon="@drawable/icon" android:label="@string/app_name">
? ? ? ? <activity android:name=".MainActivity"
? ? ? ? ? ? ? ? ? android:label="@string/app_name">
? ? ? ? ? ? <intent-filter>
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />
? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />
? ? ? ? ? ? </intent-filter>
? ? ? ? </activity>
? ? ? ? <activity android:name=".SearchResultActivity">
? ? ? ?? <intent-filter>
? ? ? ?? <action android:name="android.intent.action.SEARCH" />
? ? ? ?? </intent-filter>
? ? ? ?? <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
? ? ? ? </activity>
? ? ? ??
? ? ? ? <!-- 这个配置就可以让你在整个应用程序中调用搜索框 -->
? ?? <meta-data android:name="android.app.default_searchable" android:value=".SearchResultActivity" />
<!-- 配置搜索建议提供方 -->
<provider android:name="SearchSuggestionSampleProvider"
android:authorities="org.freedom.search.SearchSuggestionSampleProvider">
</provider>
? ? </application>
</manifest>
运行效果如下图:
?
?
?
?
?
注意:如果你的应用是直接在当前界面搜索,并在当前界面显示结果(也就是所在SearchResultActivity.java搜索,并在该界面显示)。那么上面的代码需要做如下调整。
调整后的SearchResultActivity.java文件如下:
package org.freedom.search;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.widget.TextView;
public class SearchResultActivity extends Activity {
private TextView result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = new TextView(this);
setContentView(result);
setTitle("搜索结果");
doSearch(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
doSearch(intent);
}
private void saveRecentSearchHistory(String queryKeyword) {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(queryKeyword, null);
}
private void clearRecentSearchHistory() {
SearchRecentSuggestions suggestions =new SearchRecentSuggestions(this,?
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.clearHistory();
}
private void doSearch(Intent intent) {
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String queryKeyword = intent.getStringExtra(SearchManager.QUERY);
saveRecentSearchHistory(queryKeyword);
result.setText("Your search keyword is : " queryKeyword);
}
}
@Override
protected void onDestroy() {
clearRecentSearchHistory();
super.onDestroy();
}
}
同时需要修改AndroidManifest,xml文件(-号代表删除, 号代表添加)
-<activity android:name=".SearchResultActivity">
<activity android:name=".SearchResultActivity" android:launchMode="singleTop">