原文地址: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">

更多相关文章

  1. android系统架构分析
  2. Android开发4:电话拨号器
  3. Android(安卓)开发之android架构
  4. 九大角度全方位对比Android、iOS开发
  5. Android架构
  6. Android反编译工具jadx的使用
  7. [点评]谷歌发布Android(安卓)2.3 点评八大亮点
  8. Android(安卓)使用网页开发软件界面
  9. Android(安卓)3.0真图曝光 其4.0代号为冰激凌

随机推荐

  1. 谷歌加强Android监管,你的安卓机用起来会
  2. Android传感器的使用总结
  3. Android内存管理机制之一:low memory kill
  4. Android触摸屏事件派发机制详解与源码分
  5. [android]命令行向虚拟机发短信打电话
  6. Android像素单位
  7. android:taskAffinity与android:finishOn
  8. Android使用Itext生成pdf文件
  9. Android实现多行文字的跑马灯效果(Realizi
  10. 奔五的人,准备学习iOS开发