Start new activity from SearchView
我有两个活动:第一个活动栏带有搜索视图,第二个活动栏应显示搜索查询的结果。
仙女座舱单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <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> </intent-filter> </activity> |
搜索XML
1 2 3 4 | <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/enter_a_word" /> |
搜索活动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | .... @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; } .... |
结果活性:
1 2 3 4 5 6 7 8 9 | @Override protected void onCreate(Bundle savedInstanceState) { ... if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); } ... } |
问题是,在将查询输入SearchView后,不会发生任何事情。没有错误,什么都没有。如何在SearchActivity中输入查询后打开ResultsActivity?
这个答案有点晚了,但我觉得它对未来的观众有用。这种困境似乎来自于Android SearchView教程的模糊性。它们所涵盖的场景假设您将在SearchView所在的相同活动中显示结果。在这种情况下,
1 2 3 4 5 6 7 8 9 10 | <activity android:name=".MainActivity" android:label="@string/main_activity_label" android:launchMode="singleTop"> <intent-filter> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> |
然后,要在同一个活动中处理结果,您将重写
1 2 3 4 5 6 7 8 | @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 } } |
但是,在希望在另一个活动中显示结果的情况下,必须将意向过滤器和元标记放入结果活动中,并为SearchView活动引入新的元标记。所以,在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <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> </intent-filter> </activity> |
然后,在我们的mainActivity的onCreateOptions菜单方法中,激活SearchView(假设您正在将SearchView添加到操作栏中)。与在SearchManager的getSearchableInfo()方法调用中使用
1 2 3 4 5 6 7 8 9 10 11 | @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; } |
最后,在SearchResultsActivity类中,在onCreate方法中,我们可以处理搜索结果:
1 2 3 4 5 6 7 8 | @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 } } |
不要忘记创建
searchable.xml(res/xml/searchable.xml;如果需要,在res下创建xml文件夹):
1 2 3 4 5 | <?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"/> |
布局(将SearchView作为菜单项添加到操作栏的示例):
1 2 3 4 5 6 7 8 9 10 | <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> |
资源:
- 在同一活动中显示结果
- 在不同活动中显示结果
- 组件名
我知道我也面临同样的问题,这是因为您通过传递
getComponentName()
这将由当前活动名称初始化,因此您需要使用下面给定格式的可搜索活动名称初始化它,并传递它启动新活动的相同组件实例。
1 2 3 | SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class))); search.setQueryHint(getResources().getString(R.string.search_hint)); |
希望我已经回答了这个问题!
在没有看到您的活动代码的情况下,我建议您尝试这种方法——同时假设您已经按照上面所述创建了所有文件;
在你的成果活动中,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.core_actions, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false); searchView.setQueryHint(getString(R.string.search_hint)); searchView.setOnQueryTextListener(this); return true; } |
请记住,这是一个具有您希望使其可搜索的数据的活动:
必须在同一活动中实现searchview.onquerytextListener接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: ProductsResulstActivity.this.finish(); return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { productFilterAdapter.getFilter().filter(newText); if (TextUtils.isEmpty(newText)) { listView.clearTextFilter(); } else { listView.setFilterText(newText); } return true; } |
productfilteradapter是必须事先创建的适配器。
它应该实现可过滤的接口。我希望这有帮助。
如果你需要进一步的帮助,请告诉我。祝你好运