【问题标题】:Is there a way to hide the keyboard without losing the focus of the EditText有没有办法隐藏键盘而不失去EditText的焦点
【发布时间】:2017-08-20 11:08:53
【问题描述】:

当您单击后退按钮时,键盘会自行隐藏,但编辑文本上的焦点仍然存在。我怎样才能重现这种行为?

【问题讨论】:

    标签: java android android-edittext keyboard focus


    【解决方案1】:

    您可以使用此代码隐藏键盘:

    getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    );
    

    或者这个:

    View view = this.getCurrentFocus();
    if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

    如果EditText失去焦点,简单写一个简单的命令

    editText.requestFocus();
    

    【讨论】:

    • 即使我删除 requestFocus() 部分,键盘也不会关闭。
    • requestFocus() 是将焦点放在 View 上。但是使用上面的代码来隐藏键盘。
    • 是的,但上面的代码(关闭键盘部分)也不起作用。
    • 有效!我的问题是我在关闭键盘时将可见性设置为 GONE,因此会自动移除焦点。我们的设计师建议离开可见性,它的工作原理哈哈:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多