【问题标题】:How to hide soft Keyboard without focus in Android?如何在Android中隐藏没有焦点的软键盘?
【发布时间】:2021-08-24 23:11:52
【问题描述】:

我遇到这种情况,我检查软键盘是否打开并且我想在代码中关闭,当我使用下面的代码时它无法关闭键盘,因为代码找不到任何焦点,但键盘仍然开,那我怎么隐藏呢?

private void hideSoftKeyboard() {
    Activity activity = (Activity) sContext;
    View view = activity.getCurrentFocus();
    if (view != null) {

        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        //((Activity) sContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    } else {

            Log.i(sClassTag,"focus not found");

    }
}

【问题讨论】:

  • View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }
  • InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  • 太棒了!它对我有用,谢谢!也许您可以将此移至答案,我可以投票。
  • 好的..很高兴帮助:))

标签: android keyboard


【解决方案1】:

尝试使用这个

您可以强制 Android 使用 InputMethodManager 隐藏虚拟键盘,调用 hideSoftInputFromWindow,传入包含焦点视图的窗口令牌。

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

这将强制键盘在所有情况下都隐藏。在某些情况下,您需要传入InputMethodManager.HIDE_IMPLICIT_ONLY 作为第二个参数,以确保仅在用户没有明确强制它出现时(通过按住菜单)隐藏键盘。

或者这个

InputMethodManager imm =(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

您可以找到更多详细信息here

【讨论】:

    【解决方案2】:
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    

    【讨论】:

    • 始终考虑为您的解决方案提供评论。为了解决问题,您的代码做了什么?
    【解决方案3】:

    您可以使用这些扩展来切换键盘:

    fun Context.showKeyboard() {
        val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
    }
    
    fun Context.hideKeyboard() {
        val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY)
    }
    
    fun Context.toggleKeyboard() {
        val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        if (imm.isActive) {
            hideKeyboard()
        } else {
            showKeyboard()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 2017-02-16
      • 2011-12-24
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2022-12-14
      相关资源
      最近更新 更多