像这样重写onBackPressed:
@Override
public void onBackPressed() {
if (searchView.isShown()){
searchView.onActionViewCollapsed();
searchView.setQuery("",false);
isClosed = true;
} else{
super.onBackPressed();
}
}
并且您的onCreateOptionsMenu会像这样使mSearchView膨胀:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
mSearchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnSearchClickListener(this);
mSearchView.setOnCloseListener(this);
isClosed = true;
return true;
}
你有没有像这样实现以下内容:
public class myActivity extends FragmentActivity implements
SearchView.OnQueryTextListener, View.OnClickListener, SearchView.OnCloseListener {
您还需要:
@Override
public void onClick(View view) {
isClosed = false;
}
@Override
public boolean onClose() {
isClosed = true;
return false;
}
您将需要使“ mSearchView”和“ isClosed”均为活动的全局变量。