我创建了一个简单的菜单,其中包含Search View
。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
我使用了android:launchMode="singleTop"
方法。在我执行搜索和更新UI之后,输入的文本仍然保留在那里。
我想要的是按下搜索按钮后,Search View
应将其自身恢复为:
有什么方法可以缩回它吗?
答案 0 :(得分:1)
使用@Niti Misra最初建议的内容,我必须在我的活动顶部定义SearchView searchView = null;
,而不是在onCreateOptionsMenu
内,而是在handleIntent
内。这是完整的解决方案:
public class MainActivity extends Activity {
SearchView searchView = null;
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
myString = intent.getStringExtra(SearchManager.QUERY);
searchView.onActionViewCollapsed();
searchView.setQuery("", false);
searchView.clearFocus();
doStuff();
}
}
}
答案 1 :(得分:0)
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView mSearchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
if (mSearchView!=null && searchItem!=null) {
mSearchView.clearFocus();
searchItem.collapseActionView();
}
只要您在上面的代码中提交查询以关闭actionView
答案 2 :(得分:0)
其他答案中大多数人遗漏的主键调用是搜索菜单项上的collapseActionView()
。
对setQuery
,clearFocus
等的来电确实会清除视图,但会一直显示到collapseActionView
- 这正是大多数人想要的。
以下是完整步骤(假设您已经拥有XML菜单项,包括标准SearchView Android文档中描述的actionViewClass):
private SearchView mSearchView;
private MenuItem mSearchMenuItem;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// inflate the action bar menu options, including the option that includes the search item (R.id.action_search)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// get the search menu item and target view
mSearchMenuItem = menu.findItem(R.id.action_search); // the menu item with the appropriate actionViewClass setting
mSearchView = (SearchView) mSearchMenuItem.getActionView();
// setup the Android SearchManager to automatically send the appropriate search intents
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
无论您将以下代码放入何种方法,只要您完成SearchView,请使用:
// anywhere you have a search item selected and are ready to close the search view...
// clear the search query in the toolbar so it is empty the next time the user
// opens the search view
mSearchView.setQuery("", false); // 2nd argument is false so it doesn't re-submit a blank search query
// hide the SearchView action so the toolbar returns to normal mode showing the title and other menu items, etc.
mSearchMenuItem.collapseActionView();
答案 3 :(得分:0)
searchView.setQuery("", false); // clear the text
searchView.setIconified(true); // close the search editor and make search icon again