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目录下 位置确定了 内容大该就是

view plainprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:label="@string/searchLabel" android:hint="@string/searchHint"
  4. android:icon="@drawable/menu_route" android:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider"
  5. android:queryAfterZeroResults="false" android:searchSuggestSelection=" ? ">
  6. </searchable>
[xhtml] view plaincopyprint?
  1. <?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为空的时候显示的值 相当于提示信息了

基本配置就这些 还有大量的配置是语音搜索的,不过估计这个功能真是不怎么常用吧 想要研究的就看文档吧 挺详细的 先看看吧

view plainprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:label="string resource"
  4. android:hint="string resource"
  5. android:searchMode=["queryRewriteFromData" | "queryRewriteFromText"]
  6. android:searchButtonText="string resource"
  7. android:inputType="inputType"
  8. android:imeOptions="imeOptions"
  9. android:searchSuggestAuthority="string"
  10. android:searchSuggestPath="string"
  11. android:searchSuggestSelection="string"
  12. android:searchSuggestIntentAction="string"
  13. android:searchSuggestIntentData="string"
  14. android:searchSuggestThreshold="int"
  15. android:includeInGlobalSearch=["true" | "false"]
  16. android:searchSettingsDescription="string resource"
  17. android:queryAfterZeroResults=["true" | "false"]
  18. android:voiceSearchMode=["showVoiceSearchButton" | "launchWebSearch" | "launchRecognizer"]
  19. android:voiceLanguageModel=["free-form" | "web_search"]
  20. android:voicePromptText="string resource"
  21. android:voiceLanguage="string"
  22. android:voiceMaxResults="int"
  23. >
  24. <actionkey
  25. android:keycode="KEYCODE"
  26. android:queryActionMsg="string"
  27. android:suggestActionMsg="string"
  28. android:suggestActionMsgColumn="string" >
  29. </searchable>
[xhtml] view plaincopyprint?
  1. <?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 标签中加入

view plainprint?
  1. <intent-filter>
  2. <action android:name="android.intent.action.SEARCH" />
  3. </intent-filter>
  4. <meta-data android:name="android.app.searchable"
  5. android:resource="@xml/searchable"/>
[c-sharp] view plaincopyprint?
  1. <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> 这个标签下的

view plainprint?
  1. <!-- declare the default searchable Activity for the whole app -->
  2. <meta-data android:name="android.app.default_searchable"
  3. android:value=".MySearchableActivity" />
[c-sharp] view plaincopyprint?
  1. <!-- 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()方法 如下:

view plainprint?
  1. @Override
  2. public boolean onSearchRequested() {
  3. //这个方法中干你想干的事
  4. doSometingOther();
  5. return super.onSearchRequested();
  6. }
[c-sharp] view plaincopyprint?
  1. @Overridepublic boolean onSearchRequested() {//这个方法中干你想干的事    doSometingOther();    return super.onSearchRequested();}

还有如果我们想在调用的时候传递一些参数 也是可以的

view plainprint?
  1. public boolean onSearchRequested() {
  2. Log.i(TAG,"onSearchRequested------------========");
  3. Bundle appData = new Bundle();
  4. appData.putString("key", "your info");
  5. startSearch(null, true, appData, false);
  6. return true;
  7. }
[c-sharp] view plaincopyprint?
  1. 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中 执行查询操作

view plainprint?
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.search);
  5. Intent intent = getIntent();
  6. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  7. String query = intent.getStringExtra(SearchManager.QUERY);
  8. doMySearch(query);
  9. }
  10. }
[c-sharp] view plaincopyprint?
  1. @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来实现了

view plainprint?
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.search);
  5. handleIntent(getIntent());
  6. }
  7. @Override
  8. protected void onNewIntent(Intent intent) {
  9. setIntent(intent);
  10. handleIntent(intent);
  11. }
  12. private void handleIntent(Intent intent) {
  13. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  14. String query = intent.getStringExtra(SearchManager.QUERY);
  15. doMySearch(query);
  16. }
  17. }
[c-sharp] view plaincopyprint?
  1. @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配置是这样的

view plainprint?
  1. <activity android:name=".GoogleMapActivity" android:launchMode="singleTop"
  2. android:label="@string/app_name">
  3. <intent-filter>
  4. <action android:name="android.intent.action.MAIN" />
  5. <category android:name="android.intent.category.LAUNCHER" />
  6. </intent-filter>
  7. <intent-filter>
  8. <action android:name="android.intent.action.SEARCH" />
  9. </intent-filter>
  10. <meta-data android:name="android.app.searchable"
  11. android:resource="@xml/searchable"/>
  12. </activity>
[c-sharp] view plaincopyprint?
  1. <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类

view plainprint?
  1. public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
  2. /**
  3. * Authority
  4. */
  5. final static String AUTHORITY = "com.debby.googlemap.SuggestionProvider";
  6. /**
  7. * Mode
  8. */
  9. final static int MODE = DATABASE_MODE_QUERIES;
  10. public SearchSuggestionProvider() {
  11. super();
  12. setupSuggestions(AUTHORITY, MODE);
  13. }
  14. }
[c-sharp] view plaincopyprint?
  1. 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" />

view plainprint?
  1. <provider android:name="com.debby.googlemap.SearchSuggestionProvider"
  2. android:authorities="com.debby.googlemap.SuggestionProvider" />
[c-sharp] view plaincopyprint?
  1. <provider android:name="com.debby.googlemap.SearchSuggestionProvider"            android:authorities="com.debby.googlemap.SuggestionProvider" />

这里注意 android:authorities 的配置与Provider里的保持一致就好了

这样在Acitivity里就可以调用了

view plainprint?
  1. SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
  2. SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
  3. suggestions.saveRecentQuery(query, null);
[c-sharp] view plaincopyprint?
  1. SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,                SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);suggestions.saveRecentQuery(query, null);

保存完成了 点击搜索完成后保存成功了 下次搜索就可以看到 效果看PP

那有些时候需要保存一些查询结果 例如我在地图上查询一个地点位置 那我下次查询的时候希望可以快速实现查询


这种情况就可以把上次查询的一些该地点的信息 譬如 经纬度等信息保存下来 这样就直接通过sqlit来手动保存数据

可以在handleIntent()方法中进行插入 查询操作来完成了 就是个数据库操作 不再详细实现了

就到这了 继续研究Icon那个问题

android搜索框实现相关推荐

  1. android搜索框布局文件,android搜索框列表布局,流程及主要步骤思维导图

    android搜索框列表布局,流程及主要步骤思维导图 android搜索框列表布局,流程及主要步骤思维导图 activity_coin_search.xml ---------- android:id ...

  2. [Android]搜索框SearchView

    SearchView的介绍 SearchView提供了用户界面,并且可以通过监听查询内容来帮助实现搜索查询功能的小组件. SearchView的属性 XML 属性 android:iconifiedB ...

  3. Android 搜索框的实时查询/模糊查询

    在搜索框的检索中我们经常会遇到"精确检索"."模糊检索",精确检索我就不多加解释了,我们看下模糊检索: 参考: http://blog.csdn.net/jds ...

  4. Android 搜索框:SearchView 的属性和用法详解

    转载请标明出处: http://blog.csdn.net/airsaid/article/details/51087226 本文出自:周游的博客 SearchView简介 SearchView属性 ...

  5. Android 搜索框、书架页面以及排行榜页面UI设计

    设计思路 收集各大主流小说App搜索UI设置,发现基本都会有在导航栏(AppBar)中设置搜索功能.故本App也采取这种主流设计.同时考虑在书架页面和排行榜页面进行滑动切换. 搜索框UI设计 实现效果 ...

  6. 点击android搜索框跳入另一个页面,android - ToolBar中的SearchView如何让点击之后跳转到一个新的Activity...

    PHP中文网2017-04-17 13:08:302楼 @李引证 的回答包括了关键信息,我来补充一些细节,及纠正几个细节上的错误. 关于"菜单项的点击事件" 我们需要覆写 onCr ...

  7. android 搜索框组件,Android零基础入门|搜索框组件SearchView

    原标题:Android零基础入门|搜索框组件SearchView 一.SearchView概述 SearchView是搜索框组件,它可以让用户在文本框内输入文字,并允许通过监听器监控用户输入,当用户输 ...

  8. android搜索框功能实现_巧用 Trie 树,实现搜索引擎关键词提示功能

    来源 | 码海责编 | Carol封图 | CSDN 付费下载于视觉中国我们几乎每天都在用搜索引擎搜索信息,相信大家肯定有注意过这样一个细节:当输入某个字符的时候,搜索引框底下会出现多个推荐词,如下, ...

  9. Android搜索框效果

    转载:http://blog.csdn.net/walker02/article/details/7917392 需求:项目中的有关搜索的地方,加上清空文字的功能,目的是为了增加用户体验,使用户删除文 ...

最新文章

  1. 远程打包linux成镜像,ubuntu18.04 现有系统打包成镜像
  2. Window编译Opencv CUDA
  3. C# 制作外挂常用的API
  4. tf里面InteractivateSession()与Session()的区别
  5. SAP Fiori : Response from creating in local store
  6. 时序数据库influxdb+grafana
  7. bison、lex版本不同造成的问题
  8. Struts1.x多文件上传问题
  9. 第二阶段团队站立会议08
  10. Linux查询命令帮助语句,linux有关命令的帮助和用法查看
  11. 深企在VR硬件领域获多项突破
  12. html超浪漫的3D动态相册表白网站制作 html程序员专属情人节表白网站
  13. 《游戏设计艺术(第二版)》第十章个人学习
  14. 麦咖啡企业版McAfee VirusScan Enterprise v8.8授权版
  15. 深度学习 | 误差反向传播法
  16. 谷歌浏览器翻译插件推荐——Google Chrome 插件推荐
  17. [C语言错误]expected declaration or statement at end of input)
  18. 当我们谈战略,我们究竟在谈什么?
  19. 新三级医院信息化建设解决方案
  20. 工业机器人三点工具定位法图文_手把手教你工业机器人三点示教法

热门文章

  1. 族蚂和凡科两大自助建站平台性价比哪家强?
  2. Spring Boot1.5 学习笔记
  3. 最逼近Mac OS的Linux系统 -- Elementary OS
  4. Mac终端使用Linux
  5. ClickHouse正则匹配内网IP
  6. 汇编指令-CMP、TEQ
  7. 淘宝排名查询接口,关键词排名API,宝贝排名查询,在线淘宝排名查询
  8. org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested
  9. 图文解决系列之解决Submit including parents
  10. oim(类QQ)开源项目源码阅读笔记(1)——登录部分