Android 提供了search framework来为application 提供search experience.为application提供两张方式使用search。一种是在屏幕上面的search dialog,一种是通过searchView的提供的search widget.
还可能使能voice search,提供search request等功能.
当用户执行search 是,系统会新建一个Intent来启动和search interface关联的activity.
要使用search interface,可以follow下面的步奏.
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/app_label"
android:hint="@string/search_hint" >
</searchable>
其中android:label是必须的,而android:hint 是可选的.
2.定义一个activity 来处理用户的search 需求.在<intent-filter>中必须包含ACITON_SEARCH,在meta-data中指定我们在第一步中的search 配置.
<application ... >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
...
</application>
3.我们在searchable的activity中的onCreate 函数中解释发送过来携带搜索数据的Intent
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
一般情况下用ListView来呈现搜索的结果,如果搜索的数据来自SQLite,
则应该使用CursorAdapter,如果是其他来源,则使用BaseAdapter.
通过ListActivity的子类来呈现搜索结果.
如果search dialog被激活hi,可以通过实现onSeatchRequested方法来得到通知
@Override
public boolean onSearchRequested() {
pauseSomeStuff();
return super.onSearchRequested();
}
如果你设定Android的launchMode为singleTop,则应该在onNewIntent中处理Intent.
@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);
}
}
Using the Search Widget
需要在onCreateOptionsMenu 中实现
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
当同时使用widget和dialog时,可以通过onOptionsItemSelected()来激活widget。可以使用onSearchRequested()来激活dialog.
通过在xml中增加android:voiceSearchMode 来增加voicesearch
<?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:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable> Search
还可能使能voice search,提供search request等功能.
当用户执行search 是,系统会新建一个Intent来启动和search interface关联的activity.
要使用search interface,可以follow下面的步奏.
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/app_label"
android:hint="@string/search_hint" >
</searchable>
其中android:label是必须的,而android:hint 是可选的.
2.定义一个activity 来处理用户的search 需求.在<intent-filter>中必须包含ACITON_SEARCH,在meta-data中指定我们在第一步中的search 配置.
<application ... >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
...
</application>
3.我们在searchable的activity中的onCreate 函数中解释发送过来携带搜索数据的Intent
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
一般情况下用ListView来呈现搜索的结果,如果搜索的数据来自SQLite,
则应该使用CursorAdapter,如果是其他来源,则使用BaseAdapter.
通过ListActivity的子类来呈现搜索结果.
如果search dialog被激活hi,可以通过实现onSeatchRequested方法来得到通知
@Override
public boolean onSearchRequested() {
pauseSomeStuff();
return super.onSearchRequested();
}
如果你设定Android的launchMode为singleTop,则应该在onNewIntent中处理Intent.
@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);
}
}
Using the Search Widget
需要在onCreateOptionsMenu 中实现
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
当同时使用widget和dialog时,可以通过onOptionsItemSelected()来激活widget。可以使用onSearchRequested()来激活dialog.
通过在xml中增加android:voiceSearchMode 来增加voicesearch
<?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:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable> Search