/*这个demo演示了search widget的用法,通过将该组件应用在action bar中。
* 1.创建右键菜单,将菜单项指向SearchViewandroid:actionViewClass="android.widget.SearchView"
* 2.在res/xml目录下建立searchable.xml,根节点为:searchable该文件是对searchView的基本配置
* 如搜索框中显示的提示信息,搜索建议的相关信息等等。
* 3.searchView接收与用户交互的信息,但执行搜索并处理搜索结果却在另一个activity中完成(本例为InternalSelectionView)
* 4.当用户提交搜索,系统会选择一个合适的activity并将搜索关键字附加到intent当中发送出去
* 5.SearchableActivity接收到搜索关键字并执行搜索和自理搜索结果
* 6.需要在配置文件中配置SearchableActivity添加:
* <intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>*/
public class MainActivity extends Activity {
private SearchView mSearchView;
private SearchManager mSearchManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
mSearchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
// ComponentName cn=new ComponentName(this,
// InternalSelectionView.class);
// SearchableInfo info=mSearchManager.getSearchableInfo(cn);
SearchableInfo info = mSearchManager
.getSearchableInfo(getComponentName());
// System.out.println(info.getSuggestAuthority().toString());
List<SearchableInfo> seachables = mSearchManager
.getSearchablesInGlobalSearch();
int N = seachables.size();
System.out.println(N);
for (SearchableInfo inf : seachables) {
System.out.println(inf.getSuggestAuthority().toString());
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("com.example")) {
info=inf;
}
}
mSearchView.setSearchableInfo(info);
return true;
}
}
searchable_in_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:title="search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
searchable.xml
android:includeInGlobalSearch=”true”将该searchable包含在全局中当我们使用
mSearchManager .getSearchablesInGlobalSearch();时就可以获取到该Searchable
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/search_label"
android:searchSuggestAuthority="com.example.android.apis.SuggestionProvider"
android:searchSuggestSelection=" ? "
android:includeInGlobalSearch="true" >
</searchable>
InternalSelectionView.java
public class InternalSelectionView extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchable_activity);
tv=(TextView) findViewById(R.id.result);
doSearch(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
doSearch(intent);
}
private void doSearch(Intent intent) {
if(intent==null){
//System.out.println("意图为空!");
return;
}else{
System.out.println(intent.getAction().toString());
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String text=intent.getStringExtra(SearchManager.QUERY);
// System.out.println(text);
tv.setText(text);
}
}
}
}
searchable_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是进入Search的界面" >
</TextView>
配置文件
需要注意的是android:name=”android.app.searchable”的值必须为android.app.searchable。否则我们在通过mSearchManager .getSearchableInfo(getComponentName());获取SearchableInfo时获取到的值为空。
<activity android:name="com.example.draganddrop.InternalSelectionView">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>