如果你要在你的应用程序中实现搜索功能,android中为用户提供两种搜索的特性:
一种是search dialog,另一种是search widget.
由于search widget要在3.0以上的版本才能使用。这里只讲search dialog
search dialog是由android系统控制的。需要由用户去激活它。并且搜索框只出现在activity的最顶部。当提交查询的数据时,系统会转发给一个activity进行处理。用户也可以保存最近查询的数据。这里讲一下基本的配置。
1.新建一个位于res/xml下的一个searchable.xml的配置文件
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
<!-- label为搜索框上方的文本,hint搜索框里面的提示文本 -->
android:label="@string/search_label"
android:hint="@string/search_hint"
android:icon="@drawable/search32"
android:searchMode="showSearchLabelAsBadge"
<!-- 中间是语音搜索配置(看不懂....) -->
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="Invoke Search"
<!-- 这里的值必须SearchSuggestionSampleProvider.AUTHORITY相同,后面讲 -->
android:searchSuggestAuthority="SuggestionProvider"
android:searchSuggestSelection=" ? ">
</searchable>
2.新建一个activity:SearchInvoke.java。此activity的作用是用来启动search dialog并把要查询的数据进行转发给另一个activity进行处理。
关键代码:
public class SearchInvoke extends Activity{
private Button mStartSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_invoke);
//就一个按钮
mStartSearch = (Button)findViewById(R.id.btn_start_search);
//启动搜索框
mStartSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onSearchRequested();
}
});
}
//重写onSearchRequested方法
@Override
public boolean onSearchRequested() {
//除了输入查询的值,还可额外绑定一些数据
Bundle appSearchData = new Bundle();
appSearchData.putString("demo_key","text");
startSearch(null, false, appSearchData, false);
//必须返回true。否则绑定的数据作废
return true;
}
}
3.处理activity--》SearchQueryResults.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_query_results);
//两个TextView进行数据显示
mQueryText = (TextView) findViewById(R.id.txt_query);
mAppDataText = (TextView) findViewById(R.id.txt_appdata);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onCreate()");
}
else {
mDeliveredByText.setText("onCreate(), but no ACTION_SEARCH intent");
}
}
/**
* Called when new intent is delivered.
这个方法不知道何时调用。看了文档说在manifest中配置此activity的启动模式为singleTop。不过试了貌似还没有执行到此方法
*/
@Override
public void onNewIntent(final Intent newIntent) {
super.onNewIntent(newIntent);
Log.i(TAG, "SearchQueryResults-->onNewIntent()");
// get and process search query here
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
doSearchQuery(queryIntent, "onNewIntent()");
}
else {
mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent");
}
}
//处理
private void doSearchQuery(final Intent queryIntent, final String entryPoint) {
//获取查询的值
final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);
mQueryText.setText(queryString);
//保存查询的值,这样在下次搜索的时候会有以前搜索数据的提示
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);
suggestions.saveRecentQuery(queryString, null);
//获得额外递送过来的值
final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA);
if (appData == null) {
mAppDataText.setText("<no app data bundle>");
}
if (appData != null) {
String testStr = appData.getString("demo_key");
mAppDataText.setText((testStr == null) ? "<no app data>" : testStr);
}
}
3.创建类SearchSuggestionSampleProvider.java用户查询数据保存的配置信息
public class SearchSuggestionSampleProvider extends
SearchRecentSuggestionsProvider {
final static String AUTHORITY="SuggestionProvider";
final static int MODE=DATABASE_MODE_QUERIES;
public SearchSuggestionSampleProvider(){
super();
setupSuggestions(AUTHORITY, MODE);
}
}
4.最重要的一步,在AndroidManifest.xml中的配置
<!-- search ui -->
<activity android:name="com.zyz.app.SearchQueryResults"
android:label="处理查询结果">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<!--此activity用来输入并递送结果,meta-data必须配置-->
<activity android:name="com.zyz.app.SearchInvoke">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value="com.zyz.app.SearchQueryResults"/>
</activity>
<!--provider的注册-->
<provider android:name="com.zyz.app.SearchSuggestionSampleProvider"
android:authorities="SuggestionProvider" />
5.无图无真相
- 大小: 14.4 KB
分享到:
相关推荐
SearchDialog一个Android搜索对话框库
SearchDialog的简单用法,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4226055.html
search-dialog An awesome and customizable search dialog with built-in search options. Usage First add jitpack to your projects build.gradle file allprojects { repositories { ... maven { url ...
search-dialog具有内置搜索选项的令人敬畏且可自定义的搜索对话框。 用法首先将jitpack添加到您的项目build.gradle文件中allprojects {存储库{... maven {url search-dialog一个具有内置搜索选项的超棒且可自定义的...
SearchDialog Android搜索对话框库设置1.提供gradle依赖关系将其添加到存储库末尾的root build.gradle中:allprojects {存储库{... maven {url'http SearchDialog Android搜索对话框库设置1.提供gradle依赖关系将其...
在每章的讲解中,它会让你通过一系列示例项目逐步掌握Android中的各种新功能和技术,助你取得最圆满的学习效果。本书所介绍的各个应用实例简明扼要且极具实用价值,它们覆盖了Android 1.0的所有基本功能和高级功能...
一个书中的Android编程范例,Android 日程管理专家 APP源码,主要功能有:添加日程、日程管理、日程搜索、功能设置等。创建新日程时的临时数据,只需要年月日三个数据,用来在刚刚进入新建日程界面日把年月日默认...
Android 自定义支持快速搜索筛选的选择控件使用方法,具体如下 项目中遇到选择控件选项过多,需要快速查找匹配的情况。 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz/SearchSelect 这个...
Android在应用程式中搜寻示范 分支:大师级使用seaerch小部件 添加暴力搜索 添加最近的查询建议 分支:search_dialog使用搜索对话框 分支:custom_suggestion 使用搜索小部件 添加自定义查询建议
对话模式(android:spinnerMode =“ dialog”) 可搜索模式(app:smsp_isSearchable =“ true”) 可搜索的微调器 smsp_isSearchable :默认为false。 将其设置为true以启用此功能。 smsp_enableSearchHeader :...
Searchable Spinner is a dialog spinner with the search feature which allows to search the items loaded in the spinner. Gradle dependencies { ... compile '...
在进行Android开发的过程中,免不了,要开发TCP/UDP通讯的程序,下面这两段代码,分别介绍了TCP/UCP通过的一个实例: 代码一: private void tcpdata() { try { Socket s = new Socket("192.168.0.25", 65500); ...
dependencies { // [...] implementation ' com.algolia.instantsearch:voice:1.0.0-beta02 ' // [...]}用法基本用法在“活动”中,检查您是否具有权限并显示相应的Dialog : if ( ! ...
官方网址在这儿 ...Embedded Find dialog, Text search, Document Index generation. Copy from/paste to MS Word, browsers and other applications Embedded Markdown, Pascal and HTML syntax highlighting. ...
Change Firefox Search to HTTPS (Root Only)- Changes default search to use HTTPs Check/Set IPv6 Privacy(Root Only) - Sets if your MAC address is used as part of your IPv6 address. Dialog explains more ...
Update your search paths if needed 3)....Added: Major improvements in DumpAllocationsToFile function (EMemLeaks unit) 4)....Added: MemLeaksSetParentBlock, MemLeaksOwn, EurekaTryGetMem functions ...
Support for Windows 32 bit, 64 bit, Mac OS X, iOS and Android Support for HTML formatted text, including hyperlinks in various parts of the components Built-in support for LiveBindings in ...
ICS V9 is in early development and is planned to support Android. There are no current plans for ICS for iOS. Version Control repository: --------------------------- svn://svn.overbyte.be/ics or ...