android搜索框实现 【转】
来源:互联网 发布:老王python官网 编辑:程序博客网 时间:2024/06/08 02:11
在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:hint="@string/searchHint" -
android:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider" -
android:searchSuggestSelection=" ? >" - </searchable>
- <?xml
version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/searchLabel" androidandroid:hint="@string/searchHint"android:icon="@drawable/menu_route" androidandroid:searchSuggestAuthority="com.debby.googlemap.SuggestionProvider"android:queryAfterZeroResults="false" android:searchSuggestSelection=" ? ></searchable>"
其中有个 android:icon="@drawable/menu_route" 本来以为可以设置就搜索Text前面那个View的 后来发现不起作用,而且文档中都没提到这个属性 看来确实没用啊 因为这属性我可折腾好久这个以后再说吧
还有一点要注意的就是
android:label 标签不知道有啥用 不过还要有
基本配置就这些 还有大量的配置是语音搜索的,不过估计这个功能真是不怎么常用吧 想要研究的就看文档吧 挺详细的 先看看吧
- <?xml
version="1.0" encoding="utf-8"?> - <searchable
xmlns:android="http://schemas.android.com/apk/res/android" -
resource" -
resource" -
| "queryRewriteFromText"] -
resource" -
-
-
-
-
-
n ="string" -
-
-
| "false"] -
n ="stringresource" -
| "false"] -
| "launchWebSearch" | "launchRecognizer"] -
| "web_search"] -
resource" -
-
-
-
-
-
-
-
> - </searchable>
- <?xml
version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android" resource" resource" | android:searchButtonText="string"queryRewriteFromText"] resource" n ="string"| android:searchSettingsDescriptio"false"] n ="stringresource" | android:voiceSearchMode=["showVoiceSearchButton""false"] | android:voiceLanguageModel=["free-form""launchWebSearch" | "launchRecognizer"] | android:voicePromptText="string"web_search"] resource" >
二、创建一个搜索功能的Activity
- <intent-filter>
-
<action android:name= /> -
</intent-filter> -
<meta-data android:name= -
android:resource=
- <intent-filter>
<action android:name= /> "android.app.searchable"</intent-filter> <meta-data android:name= android:resource=
就可以了那这样的配置也就是只在这个Activity中可以使用搜索功能,其实Android的搜索框可以支持整个应用Application
这样就需要创建一个专门处理搜索的Acitivity 可以这样配置需要在<application></application> 这个标签下的
- <!--
declare defaultthe searchable forActivity the whole app --> -
<meta-data android:name= -
android:value= />
- <!--
declare defaultthe searchable forActivity the "android.app.default_searchable"whole app --> <meta-data android:name= android:value= />
三、调用搜索框 现在已经配置完成了 下面就可以开始调用了
调用的方法很简单 所有的Acitivity都可以调用onSearchRequested() 方法 这样搜索框就出现了,那测试的时候可以有个简单的方法
在onCreate()方法中调用setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL),这样,当用户按下键盘上的按键时,将会自动激活搜索框
如果你要在执行搜索时,进行别的操作,可以重写onSearchRequested()方法 如下:
- @Override
- public
boolean onSearchRequested() { - //这个方法中干你想干的事
-
doSometingOther(); -
super.onSearchRequested(); - }
- @Overridepublic
boolean //这个方法中干你想干的事onSearchRequested() { doSometingOther(); return super.onSearchRequested();}
还有如果我们想在调用的时候传递一些参数 也是可以的
- public
boolean onSearchRequested() { -
Log.i(TAG, -
Bundle appData = Bundle(); -
appData.putString( "your info" ); -
startSearch( true, appData, false); -
true; -
}
- public
boolean "onSearchRequested------------========");onSearchRequested() { Log.i(TAG, Bundle appData = Bundle(); "key",appData.putString( "your info" );startSearch( true, appData, false);true; }
四、接受查询条件并执行查询
如果是创建了专门处理查询的Acitivity 当然可以直接在onCreate中 执行查询操作
- @Override
- public
void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.search); -
Intent intent = getIntent(); -
(Intent.ACTION_SEARCH.equals(intent.getAction())) { -
String query = intent.getStringExtra(SearchManager.QUERY); -
doMySearch(query); -
} - }
- @Overridepublic
void onCreate(Bundle ifsavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); Intent intent = getIntent(); (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) { -
(Intent.ACTION_SEARCH.equals(intent.getAction())) { -
String query = intent.getStringExtra(SearchManager.QUERY); -
doMySearch(query); -
} - }
- @Overridepublic
void onCreate(Bundle voidsavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search); handleIntent(getIntent());} @Overrideprotected onNewIntent(Intent privateintent) { setIntent(intent); handleIntent(intent);} void handleIntent(Intent ifintent) { (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= -
<intent-filter> -
<action android:name= /> -
<category android:name= /> -
</intent-filter> -
<intent-filter> -
<action android:name= /> -
</intent-filter> -
<meta-data android:name= -
android:resource= -
</activity>
- <activity
android:name= ".GoogleMapActivity"android:launchMode= "singleTop"android:label= <intent-filter> <action android:name= /> "android.intent.category.LAUNCHER"<category android:name= /> "android.intent.action.SEARCH"</intent-filter> <intent-filter> <action android:name= /> "android.app.searchable"</intent-filter> <meta-data android:name= android:resource= </activity>
五、纪录历史关键字 我们在查询完成后会希望保存这次查询的条件 甚至有的会连结果都保存了
首先创建一个Provider类
- public
class SearchSuggestionProvider extends SearchRecentSuggestionsP rovider { -
-
final String "com.debby.googlemap.SuggestionProvider";AUTHORITY = -
-
final int MODE = DATABASE_MODE_QUERIES; -
SearchSuggestionProvider () { -
super(); -
setupSuggestions(AUTHORITY, MODE); -
} - }
- public
class SearchSuggestionProvider extends SearchRecentSuggestionsP rovider { final String "com.debby.googlemap.SuggestionProvider";AUTHORITY = final int MODE public= DATABASE_MODE_QUERIES; SearchSuggestionProvider () { super(); setupSuggestions(AUTHORITY, MODE); }}
当然还要在 Manifest中配置
<provider android:name="com.debby.googlemap.SearchSuggestionProvider
- <provider
android:name= "com.debby.googlemap.SearchSuggestionProvider" -
android:authorities= />
- <provider
android:name= "com.debby.googlemap.SearchSuggestionProvider" android:authorities= />
这里注意 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,
保存完成了 点击搜索完成后保存成功了 下次搜索就可以看到 效果看PP
那有些时候需要保存一些查询结果例如我在地图上查询一个地点位置 那我下次查询的时候希望可以快速实现查询
这种情况就可以把上次查询的一些该地点的信息 譬如经纬度等信息保存下来 这样就直接通过sqlit来手动保存数据
可以在handleIntent()方法中进行插入查询操作来完成了 就是个数据库操作 不再详细实现了
- android搜索框实现 【转】
- android搜索框实现
- android搜索框实现
- android搜索框实现
- android搜索框功能实现
- android顶部(toolbar)搜索框实现
- Android搜索框之PopupWindow+ListView实现
- Android 用EditText实现搜索框功能
- android顶部(toolbar)搜索框实现
- Android 下拉搜索提示框的实现
- Android搜索功能实现
- android实现关键字搜索功能
- android中实现内容搜索
- android中实现内容搜索
- Android实现多功能搜索条
- android 蓝牙搜索功能实现
- Android中实现快速搜索
- Android搜索功能的实现
- spring3+hibernate3
- 国外程序员推荐:每个程序员都应读的书
- DB2 ODBC 编程概述(函数参数详解)
- iphone——使用run loop对象
- WPF媒体播放器(MediaElement)打开指定的视频、播放、暂停、快进、快退、截图
- android搜索框实现 【转】
- tomcat设置jre_home
- 心思心语
- spring ioc原理
- 关于ALV GRID的颜色
- 动画监听
- js apply/call/caller/callee/bind使用方法与区别分析
- xmlbean 生成带包路径的jar
- 如何动态的获取和设置ImageView的宽度和高度?