应用兼容 v21.如何通过后退按钮折叠搜索视图



使用旧的appcompat,我们在ActionBar中使用了SearchView,当我按"返回"按钮时,我会折叠SearchView

但现在我切换到Appcompat v21并使用ToolBar代替它。但是现在SearchView当我按下"返回"按钮时不会崩溃。

如何解决这个问题?

此致敬意。

我遇到了同样的问题,我按如下方式解决了它:
1-让您的活动或片段实现SearchView.OnFocusChangeListener
2-添加mSearchView.setOnQueryTextFocusChangeListener(this);
3-添加

@Override
 public void onFocusChange(View v, boolean hasFocus) {
    if(!hasFocus) {
         MenuItemCompat.collapseActionView(searchMenuItem);
    }
}

它对我有用:)

使用

searchItem.collapseActionView();

你可以覆盖onBackPressed()方法:

  @Override
public void onBackPressed()
{
     super.onBackPressed();  // optional depending on your needs
     finish();
}

这应该可以解决问题。

最新更新