当你需要在应用程序中提供搜索服务时,你第一个想到的是搜索框要放哪呢?其实Android已经为我们提供了一个浮动搜索框。Android手机上有一个专门的搜索按钮,用于处理搜索服务(当然有的定制版阉割了此按钮)。下面通过一个简单的搜索示例,展示Android浮动搜索框的用法。
其中
- 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">
相关推荐
android 浮动搜索框 在我们博客http://blog.csdn.net/a253664942/article/details/45728981中有详细的介绍,有问题可以提问
android浮动搜索框示例
android源码实现一个搜索浮动框示例,演示android系统浮动搜索框,主要是借助Android原有系统的浮动框实现,学习一下如何调用Android原生的组件等资源,初学者必看的小程序。
android浮动搜索框(SearchManager)
Android调用系统的浮动搜索框实现搜索功能,压缩包中含有两个实例,和一个文档。里面包含详细的操作步骤
* 演示android系统浮动搜索框 * 让程序开发不再那么神秘
Android浮动搜索框源码,带语音搜索和文字搜索功能,适合初学者
文档介绍了前言,基础知识,创建一个搜索对话框配置文件,创建一个可用于搜索的Activity,定义一个可搜索的Activity,执行一个搜索,接收搜索查询,调用搜索对话框,如何给搜索框增加参数
Android---浮动搜索框(SearchManager)(An-Beer工作室)借鉴.pdf
适合学习Android的同学和基于毕业生学习和参考,Android studio、idea、eclipse可以运行,建议使用Android studio或者idea
浮动搜索框(SearchManager).zip
1、弹出浮动的搜索框,并能搜索地址并定位,显示位置地址 2、长按地图出现当前位置的泡泡(popup),泡泡里有标题和内容,有详细地址和详细信息 3、通过GPS,基站等自动定位到当前位置 4、显示各种图层
选择任何文本,然后使用选择工具栏直接搜索应用程序。版本支持Diction支持高于6.0(API 23)及更高版本的android版本。屏幕截图执照Diction已获得Apache License 2.0的许可Copyright 2018 Ravi Tamang.Licensed ...
从 Android 3.0 开始,不推荐使用“菜单”按钮(有些设备没有),因此您应该转向使用操作栏来提供对操作和其他选项的访问。 请参阅有关创建选项菜单的部分。 ####上下文菜单和上下文操作模式上下文菜单是一个浮动...
在工具栏中通过SearchView搜索RecycleView 版本 1.0 科技类 InfiniteRecycleView使用许多android库来正常工作: com.android.support:design:22.2.1 com.android.support:recyclerview-v7:22.2.1 ...