重置search小部件(SearchView)值

我有两个活动:首先, SearchActivity活动,有一个search小部件,使用其他活动,search活动search数据。

我想要做的是,当我从SearchActiviy返回到SearchActiviy ,search小部件崩溃,并与空文本。

我试图做以下事情:

 public class HomepageActivity extends Activity { @TargetApi(11) @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.projectslist, menu); if(Build.VERSION.SDK_INT >= 11) { SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.homepage_search).getActionView(); ComponentName component = new ComponentName(this, SearchActivity.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(component)); searchView.setIconifiedByDefault(true); searchView.setQuery("", false); } return super.onCreateOptionsMenu(menu); } […] @TargetApi(11) @Override protected void onRestart() { super.onRestart(); if(Build.VERSION.SDK_INT >= 11) invalidateOptionsMenu(); launchAsynchronousImageDownload(); } } 

如果小部件显示为折叠状态,那么小部件中的文本仍会记住search到的文本(重新打开小部件之后)。 我怎样才能重置小部件的文字?

谢谢你的帮助! ;)

Solutions Collecting From Web of "重置search小部件(SearchView)值"

您也可以尝试以下方法:

 searchView.setQuery("", false); searchView.clearFocus(); 

这是魔术

searchView.setQuery( “”,假); //清除文字

searchView.setIconified(真); //closuressearch编辑器并重新search图标

在创build函数之后在主动插入onSaveInstanceState函数

此function将触发每次您打开一个新的活动, 在打开新的活动之前,它将重置search窗口小部件的值

  protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); searchView.setQuery("", false); searchView.setIconified(true); } 

这对我工作:

首先,我在活动的顶部声明了菜单项variables:

 private MenuItem mSearchMenuItem; 

我在OnCreateOptionsMenu()中定义了variables:

 mSearchMenuItem = menu.findItem(R.id.action_search); 

我在onResume()中声明了invalidateOptionsMenu():

 @Override protected void onResume() { invalidateOptionsMenu(); super.onResume(); } 

最后,我在onPrepareOptionsMenu()的菜单项上调用了collapseActionView()。

 @Override public boolean onPrepareOptionsMenu(Menu menu) { mSearchMenuItem.collapseActionView(); return super.onPrepareOptionsMenu(menu); } 
 searchView.setQuery("", false); searchView.setIconified(false); 

我也有这个问题,如果我把它放在onPrepareOptionsMenu它工作。

 @Override public boolean onPrepareOptionsMenu (Menu menu) { SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setQuery("", false); // rest of code... } 

刚刚find一个丑陋的方式,使其工作(阅读意见,以查看差异):

 public class HomepageActivity extends Activity { // Declaring SearchView as an instance object private SearchView searchView; @TargetApi(11) @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.projectslist, menu); if(Build.VERSION.SDK_INT >= 11) { SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); // Using instance var instead of local var searchView = (SearchView) menu.findItem(R.id.homepage_search).getActionView(); ComponentName component = new ComponentName(this, SearchActivity.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(component)); searchView.setIconifiedByDefault(true); // Setting query is not anymore required //searchView.setQuery("", false); } return super.onCreateOptionsMenu(menu); } […] @TargetApi(11) @Override protected void onRestart() { super.onRestart(); // Do not need to recreate menu /*if(Build.VERSION.SDK_INT >= 11) invalidateOptionsMenu();*/ if(Build.VERSION.SDK_INT >= 11) { // Calling twice: first empty text field, second iconify the view searchView.setIconified(true); searchView.setIconified(true); } launchAsynchronousImageDownload(); } } 

我觉得这很丑陋,所以如果有人认为是更好的主意,那就告诉我:)