【问题标题】:Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out当我单击硬件后退按钮并且键盘不在时,上下文操作栏会隐藏
【发布时间】:2014-04-17 09:17:16
【问题描述】:

我正在使用上下文操作栏进行编辑,当我显示键盘并且我想通过按下硬件后退按钮来隐藏它时,它隐藏了键盘,但它也取消了上下文操作栏,我真的找不到如何保持它。

有人吗?

【问题讨论】:

  • 键盘已经启动,因为用户正在输入内容。他想隐藏键盘。所以他点击后退按钮。但是该操作也会调用上下文操作栏来取消自身...
  • 不,当用户单击某个项目以编辑该项目时,我会打开一个新片段。 CAB 应该在该片段中的整个时间内保持可见。就像您在谷歌驱动器中打开文档时一样。只有我认为这是一种常规行为 - 每当您显示 CAB 并且键盘已打开并且您想要隐藏键盘时,您按下后退按钮并且上下文操作栏应该知道后退是针对键盘的,不适用于 CAB。同样,它在 Google Drive 应用程序中的工作方式是这样的。
  • 我不认为你超出了指导方针。用户调用CAB时,一般是在当前activity(或fragment)中搜索一些东西。所以要看到效果他必须把键盘藏起来。此外,当用户没有找到预期的结果时,他会使用相同的 CAB 重新搜索。那么,不,我认为你没有超出指导方针。

标签: android android-softkeyboard back cab contextual-action-bar


【解决方案1】:

您应该尝试覆盖Back Key 硬件,并使用boolean 处理预期行为,如下所示:

// boolean isVisible to retrieve the state of the SoftKeyboard
private boolean isVisible = false;

// isVisible becomes 'true' if the user clicks on EditText

// then, if the user press the back key hardware, handle it:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // check isVisible
        if(isVisible) {
            // hide the keyboard
            InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            isVisible = false;
        } else {
            // remove the CAB
            mActionMode.finish();
        }
    }
    return false;
}  

另一种解决方案可能是调用dispatchKeyEvent 方法,当显示CAB 时仍会调用该方法:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        // check CAB active and isVisible softkeyboard
        if(mActionModeIsActive && isVisible) {
            InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            isVisible = false;
            return true;
        // Maybe you might do not call the 'else' condition, anyway..
        } else {
            mActionMode.finish();
            return true;
        }
    }
    return super.dispatchKeyEvent(event);
}  

这应该可以解决问题,但我还没有测试过。希望这会有所帮助。
来源:How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button

【讨论】:

  • 谢谢,我会调查的。请参阅我对原始问题的最后评论。我认为这是一个全球性问题,我认为我没有做任何违反指南的事情..
猜你喜欢
  • 2021-03-23
  • 2012-11-15
  • 2017-09-20
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多