android搜索框实现
http://blog.csdn.net/pengjianbosoft/article/details/6638402
在Map应用中会经常见到一个浮动的搜索框 一般可以搜索附近的POI点信息 而且这些功能基本都长得差不多 所以网上查了下原来在SDK 文档里就有 在Dev Guide中有详细的介绍 不过都是英文的 看了好久呢
功能是比较简单的 就是配置起来有点麻烦 下面详细说一下
首先看效果
就这样简单 首先来看配置:
一、搜索框配置文件是一个用来配置您的应用程序中搜索框的设置的XML文件,这个文件一般命名为searchable.xml,并且保存在项目的res/xml/目录下。 配置文件的根节点必须为searchable,可以有一个或多个属性。
可以发现在SearchableInfo中 android是通过com.android.internal.R.styleable.Searchable 这个ID来获取这个配置文件的
这个Searchable应该就是标签的名字,所以必须这么命名,至于文件名不重要了 文档中说must besaved in the res/xml/
project directory 也就这个文件名不重要 但这个文件必须放在XML目录下 位置确定了 内容大该就是
- <?xml version="1.0" encoding="utf-8"?>
- <searchable xmlns:android="http://schemas.android.com/apk/res/android"
- android:label="@string/searchLabel" android:hint="@string/searchHint"
- android:icon="@drawable/menu_route" android:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider"
- android:queryAfterZeroResults="false" android:searchSuggestSelection=" ? ">
- </searchable>
- <?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/searchLabel" android:hint="@string/searchHint"android:icon="@drawable/menu_route" android:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider"android:queryAfterZeroResults="false" android:searchSuggestSelection=" ? "></searchable>
其中有个 android:icon="@drawable/menu_route" 本来以为可以设置 就搜索Text前面那个View的 后来发现不起作用,而且文档中都没提到这个属性 看来确实没用啊 因为这属性我可折腾好久 这个以后再说吧
还有一点要注意的就是 android:label android:hint 属性不能直接写值 而是要points to a stringresource 就是用配置在values里的
android:label 标签不知道有啥用 不过还要有 android:hint 就是TextView为空的时候显示的值 相当于提示信息了
基本配置就这些 还有大量的配置是语音搜索的,不过估计这个功能真是不怎么常用吧 想要研究的就看文档吧 挺详细的 先看看吧
- <?xml version="1.0" encoding="utf-8"?>
- <searchable xmlns:android="http://schemas.android.com/apk/res/android"
- android:label="string resource"
- android:hint="string resource"
- android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
- android:searchButtonText="string resource"
- android:inputType="inputType"
- android:imeOptions="imeOptions"
- android:searchSuggestAuthority="string"
- android:searchSuggestPath="string"
- android:searchSuggestSelection="string"
- android:searchSuggestIntentAction="string"
- android:searchSuggestIntentData="string"
- android:searchSuggestThreshold="int"
- android:includeInGlobalSearch=["true" | "false"]
- android:searchSettingsDescription="string resource"
- android:queryAfterZeroResults=["true" | "false"]
- android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
- android:voiceLanguageModel=["free-form" | "web_search"]
- android:voicePromptText="string resource"
- android:voiceLanguage="string"
- android:voiceMaxResults="int"
- >
- <actionkey
- android:keycode="KEYCODE"
- android:queryActionMsg="string"
- android:suggestActionMsg="string"
- android:suggestActionMsgColumn="string" >
- </searchable>
- <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="string resource" android:hint="string resource" android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"] android:searchButtonText="string resource" android:inputType="inputType" android:imeOptions="imeOptions" android:searchSuggestAuthority="string" android:searchSuggestPath="string" android:searchSuggestSelection="string" android:searchSuggestIntentAction="string" android:searchSuggestIntentData="string" android:searchSuggestThreshold="int" android:includeInGlobalSearch=["true" | "false"] android:searchSettingsDescription="string resource" android:queryAfterZeroResults=["true" | "false"] android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"] android:voiceLanguageModel=["free-form" | "web_search"] android:voicePromptText="string resource" android:voiceLanguage="string" android:voiceMaxResults="int" > <actionkey android:keycode="KEYCODE" android:queryActionMsg="string" android:suggestActionMsg="string" android:suggestActionMsgColumn="string" > </searchable>
二、创建一个搜索功能的Activity
这里的Activity可以新建一个 当然也可以是当前弹出搜索框的Acitvity 这里我用到的MapAcitivity就是要显示搜索结果的 所以这里就直接用这个Acitivity了
那这两种方式实现差不多 不过也有点小小的差别 下面来看:
首先配是一样的 就是在Acitivity 标签中加入
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <meta-data android:name="android.app.searchable"
- android:resource="@xml/searchable"/>
- <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
就可以了那这样的配置也就是只在这个Activity中可以使用搜索功能,其实Android的搜索框可以支持整个应用Application
这样就需要创建一个专门处理搜索的Acitivity 可以这样配置 需要在<application></application> 这个标签下的
- <!-- declare the default searchable Activity for the whole app -->
- <meta-data android:name="android.app.default_searchable"
- android:value=".MySearchableActivity" />
- <!-- declare the default searchable Activity for the whole app --> <meta-data android:name="android.app.default_searchable" android:value=".MySearchableActivity" />
三、调用搜索框 现在已经配置完成了 下面就可以开始调用了
调用的方法很简单 所有的Acitivity都可以调用onSearchRequested() 方法 这样搜索框就出现了 ,那测试的时候可以有个简单的方法
在onCreate()方法中调用setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL),这样,当用户按下键盘上的按键时,将会自动激活搜索框
如果你要在执行搜索时,进行别的操作,可以重写onSearchRequested()方法 如下:
- @Override
- public boolean onSearchRequested() {
- //这个方法中干你想干的事
- doSometingOther();
- return super.onSearchRequested();
- }
- @Overridepublic boolean onSearchRequested() {//这个方法中干你想干的事 doSometingOther(); return super.onSearchRequested();}
还有如果我们想在调用的时候传递一些参数 也是可以的
- public boolean onSearchRequested() {
- Log.i(TAG,"onSearchRequested------------========");
- Bundle appData = new Bundle();
- appData.putString("key", "your info");
- startSearch(null, true, appData, false);
- return true;
- }
- public boolean onSearchRequested() { Log.i(TAG,"onSearchRequested------------========"); Bundle appData = new Bundle(); appData.putString("key", "your info"); startSearch(null, true, appData, false); return true; }
四、接受查询条件 并执行查询
如果是创建了专门处理查询的Acitivity 当然可以直接在onCreate中 执行查询操作
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.search);
- Intent intent = getIntent();
- if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- doMySearch(query);
- }
- }
- @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doMySearch(query); }}
但如果是在当前的Acitivity上这样就不行了 应为onCreate就执行一次 这样就可以通过onNewIntent来实现了
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.search);
- handleIntent(getIntent());
- }
- @Override
- protected void onNewIntent(Intent intent) {
- setIntent(intent);
- handleIntent(intent);
- }
- private void handleIntent(Intent intent) {
- if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- doMySearch(query);
- }
- }
- @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); handleIntent(getIntent());} @Overrideprotected void onNewIntent(Intent intent) { setIntent(intent); handleIntent(intent);} private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doMySearch(query); }}
这样就会通过doMySearch()完成了查询操作了 不过还有点需要注意 查询完成后我按返回发现还是这个Acitivity 不过是查询前的
这说明在Activity栈里有俩我的这个MapAcitivity实例 这个可以通过在Acitivity里android:launchMode=”singleTop”这样的配置解决
我的Acitivity配置是这样的
- <activity android:name=".GoogleMapActivity" android:launchMode="singleTop"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <meta-data android:name="android.app.searchable"
- android:resource="@xml/searchable"/>
- </activity>
- <activity android:name=".GoogleMapActivity" android:launchMode="singleTop" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </activity>
五、纪录历史关键字 我们在查询完成后会希望保存这次查询的条件 甚至有的会连结果都保存了
android这里实现了保存关键字的功能 是通过SearchRecentSuggestionsProvider 来实现的
首先创建一个Provider类
- public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
- /**
- * Authority
- */
- final static String AUTHORITY = "com.debby.googlemap.SuggestionProvider";
- /**
- * Mode
- */
- final static int MODE = DATABASE_MODE_QUERIES;
- public SearchSuggestionProvider() {
- super();
- setupSuggestions(AUTHORITY, MODE);
- }
- }
- public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider { /** * Authority */ final static String AUTHORITY = "com.debby.googlemap.SuggestionProvider"; /** * Mode */ final static int MODE = DATABASE_MODE_QUERIES; public SearchSuggestionProvider() { super(); setupSuggestions(AUTHORITY, MODE); }}
当然还要在 Manifest中配置
<provider android:name="com.debby.googlemap.SearchSuggestionProvider"
android:authorities="com.debby.googlemap.SuggestionProvider" />
- <provider android:name="com.debby.googlemap.SearchSuggestionProvider"
- android:authorities="com.debby.googlemap.SuggestionProvider" />
- <provider android:name="com.debby.googlemap.SearchSuggestionProvider" android:authorities="com.debby.googlemap.SuggestionProvider" />
这里注意 android:authorities 的配置与Provider里的保持一致就好了
这样在Acitivity里就可以调用了
- SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
- SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
- suggestions.saveRecentQuery(query, null);
- SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);suggestions.saveRecentQuery(query, null);
保存完成了 点击搜索完成后保存成功了 下次搜索就可以看到 效果看PP
那有些时候需要保存一些查询结果 例如我在地图上查询一个地点位置 那我下次查询的时候希望可以快速实现查询
这种情况就可以把上次查询的一些该地点的信息 譬如 经纬度等信息保存下来 这样就直接通过sqlit来手动保存数据
可以在handleIntent()方法中进行插入 查询操作来完成了 就是个数据库操作 不再详细实现了
就到这了 继续研究Icon那个问题
android搜索框实现相关推荐
- android搜索框布局文件,android搜索框列表布局,流程及主要步骤思维导图
android搜索框列表布局,流程及主要步骤思维导图 android搜索框列表布局,流程及主要步骤思维导图 activity_coin_search.xml ---------- android:id ...
- [Android]搜索框SearchView
SearchView的介绍 SearchView提供了用户界面,并且可以通过监听查询内容来帮助实现搜索查询功能的小组件. SearchView的属性 XML 属性 android:iconifiedB ...
- Android 搜索框的实时查询/模糊查询
在搜索框的检索中我们经常会遇到"精确检索"."模糊检索",精确检索我就不多加解释了,我们看下模糊检索: 参考: http://blog.csdn.net/jds ...
- Android 搜索框:SearchView 的属性和用法详解
转载请标明出处: http://blog.csdn.net/airsaid/article/details/51087226 本文出自:周游的博客 SearchView简介 SearchView属性 ...
- Android 搜索框、书架页面以及排行榜页面UI设计
设计思路 收集各大主流小说App搜索UI设置,发现基本都会有在导航栏(AppBar)中设置搜索功能.故本App也采取这种主流设计.同时考虑在书架页面和排行榜页面进行滑动切换. 搜索框UI设计 实现效果 ...
- 点击android搜索框跳入另一个页面,android - ToolBar中的SearchView如何让点击之后跳转到一个新的Activity...
PHP中文网2017-04-17 13:08:302楼 @李引证 的回答包括了关键信息,我来补充一些细节,及纠正几个细节上的错误. 关于"菜单项的点击事件" 我们需要覆写 onCr ...
- android 搜索框组件,Android零基础入门|搜索框组件SearchView
原标题:Android零基础入门|搜索框组件SearchView 一.SearchView概述 SearchView是搜索框组件,它可以让用户在文本框内输入文字,并允许通过监听器监控用户输入,当用户输 ...
- android搜索框功能实现_巧用 Trie 树,实现搜索引擎关键词提示功能
来源 | 码海责编 | Carol封图 | CSDN 付费下载于视觉中国我们几乎每天都在用搜索引擎搜索信息,相信大家肯定有注意过这样一个细节:当输入某个字符的时候,搜索引框底下会出现多个推荐词,如下, ...
- Android搜索框效果
转载:http://blog.csdn.net/walker02/article/details/7917392 需求:项目中的有关搜索的地方,加上清空文字的功能,目的是为了增加用户体验,使用户删除文 ...
最新文章
- AOSuite V2.7 发布,JavaEE快速开发平台
- JQuery-学习笔记04【基础——JQuery基础案例】
- python心理学实验程序_psychopy coder模式编写心理试验程序 字符程序和记录反应时...
- python logging日志模块的使用
- 看完这篇,你还不能理解 ‘数据库架构’?趁早回家吧
- 笑到打鸣~ | 今日趣图
- java不适合开发cv吗_JavaCV开发环境的配置
- Python高级用法总结
- 产品经理学SQL(二)
- 安利几款语音识别软件
- linux下alias命令详解
- php mongodb 管理工具,细数MongoDB管理工具
- Linux中共享库(so)的几个名称及相关用法
- 南邮算法分析与设计实验4 密码算法
- PDA用ActiveSync同步上网 方法
- 6 安卓高德地图错误代码_高德开放平台
- 【ant.design】解决Instance created by `useForm` is not connected to any Form element.
- Postman 接口神器
- 超大日志文件分割方法
- 光伏电子工程的设计与实施2022——单片机力控部分实现思路
热门文章
- echarts 折线图折线从头开始
- 有道云笔记网页剪chrome登录不上,反复登录问题
- Xanadu:逐梦一个关于光量子计算的“新上都计划”
- 为什么我的淘宝排名会突然下降?
- 美丽的窗花java分形_活动设计——美丽的窗花 (设计意图及教学反思)
- 摔倒检测+yolov5
- Pandownload盘神回来了
- put命令here are 2 datanode(s) running and 2 node(s) are excluded in this operation.的解决方法
- 《LoadRunner 12七天速成宝典》目录—导读
- Mysql——分组统计