Maa*_*ten 16 android searchview
我有2个活动:第一个有一个带有搜索视图的操作栏,第二个应该显示搜索查询的结果.
androidmanifest:
<activity
android:name=".SearchActivity"
...
android:launchMode="singleTop">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
...
</activity>
<activity
android:name=".ResultsActivity"
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
searchable.xml
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/enter_a_word" />
Run Code Online (Sandbox Code Playgroud)
SearchActivity
....
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_noun_list, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
return true;
}
....
Run Code Online (Sandbox Code Playgroud)
ResultsActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
...
}
Run Code Online (Sandbox Code Playgroud)
问题是在查询输入搜索视图后,没有任何反应.没有错误,没有.在查询中输入查询后,如何打开结果活动?
chR*_*NaN 31
这个答案有点晚,但我觉得它对未来的观众有用.这种困境似乎来自Android SearchView教程的含糊不清.它们涵盖的场景假定您将在SearchView所在的同一个Activity中显示结果.在这种情况下,AndroidManifest.xml文件中的Activity标记看起来像这样:
<activity
android:name=".MainActivity"
android:label="@string/main_activity_label"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
Run Code Online (Sandbox Code Playgroud)
然后,要在同一个Activity中处理结果,您将覆盖该onNewIntent方法:
@Override
public void onNewIntent(Intent intent){
setIntent(intent);
if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//now you can display the results
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在我们想要在另一个Activity中显示结果的情况下,我们必须将Intent Filter和meta标记放入结果Activity中,并为SearchView Activity引入一个新的元标记.所以,我们的活动在AndroidManifest.xml文件中看起来像这样:
<activity
android:name=".MainActivity"
android:label="@string/main_activity_label"
android:launchMode="singleTop">
<!-- meta tag points to the activity which displays the results -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchResultsActivity"
android:label="@string/results_activity_label"
android:parentActivityName="com.example.MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
<!-- meta tag and intent filter go into results activity -->
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
然后,在我们的MainActivity的onCreateOptionsMenu方法中,激活SearchView(假设您将SearchView添加到ActionBar).getComponentName()我们使用MainActivity的上下文和SearchResultsActivity类来实例化一个新的ComponentName对象,而不是在SearchManager的getSearchableInfo()方法调用中使用:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
search.setQueryHint(getResources().getString(R.string.search_hint));
return true;
}
Run Code Online (Sandbox Code Playgroud)
最后,在我们的SearchResultsActivity类中,在onCreate方法中,我们可以处理搜索结果:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记创建searchable.xml资源文件并将SearchView添加到您的布局中.
searchable.xml(res/xml/searchable.xml;如果需要,在res下创建xml文件夹):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
Run Code Online (Sandbox Code Playgroud)
布局(将SearchView添加到ActionBar作为菜单项的示例):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.MainActivity">
<group android:checkableBehavior="single">
<item android:id="@+id/action_search" android:title="Search"
android:orderInCategory="1" app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</group>
</menu>
Run Code Online (Sandbox Code Playgroud)
资源:
| 归档时间: |
|
| 查看次数: |
10516 次 |
| 最近记录: |