【问题标题】:How to close Android Soft KeyBoard programmatically?如何以编程方式关闭 Android 软键盘?
【发布时间】:2012-01-09 07:12:51
【问题描述】:

我目前正在使用以下代码显示软键盘

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

这里我没有用 Edittext 绑定软键盘,因为我使用了上面的代码。

现在我想关闭软键盘,所以我目前正在使用下面的代码,但它不起作用。

imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

谁能建议我用什么来关闭软键盘?


根据下面的答案,我想让您清楚我没有使用 EditText,我使用的是要在其上显示键盘和隐藏键盘的布局。我想将键盘按键事件发送到我没有使用editText的远程区域。

【问题讨论】:

  • 隐藏键盘的代码与显示键盘的代码相同。
  • 是的,但我不知道用什么来代替那个..
  • 检查this线程。
  • 这对我不起作用..请阅读上面的编辑
  • /* 在一行中隐藏软键盘 */ ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(),0);

标签: android android-softkeyboard


【解决方案1】:

我已经测试过,这是有效的:

...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

对了,你的代码第二个参数不对,请看here

【讨论】:

  • 为什么显示和隐藏使用相同的代码行?
  • 根据stackoverflow.com/q/3568919/3990767stackoverflow.com/q/4745988/3990767,很难检查键盘是否显示,因此如果您不知道它是否已经显示,切换可能不是一个好主意。
  • 这种方法的问题是,如果用户已经隐藏了键盘,第二次调用会再次显示键盘
  • 只有解决方案对我有用 +1
【解决方案2】:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);

【讨论】:

  • @Mak 然后使用:getCurrentFocus().getWindowToken()
【解决方案3】:

使用这个工作代码:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

【讨论】:

  • 它有效,谢谢。我想在用户单击登录按钮时隐藏键盘,在这种情况下,此解决方案比imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) 效果更好。因为在横屏手机中,键盘覆盖了登录按钮,用户必须手动隐藏键盘。
【解决方案4】:

如果你愿意,你可以使用整个类并在任何地方调用 KeyboardUtil.hideKeyBoard(context) 方法:

public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
    {
        try
        {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
         }
        catch (Exception e)
        {
            // Ignore exceptions if any
                Log.e("KeyBoardUtil", e.toString(), e);
        }
    }
}

【讨论】:

  • 一个始终有效并发送标志“HIDE_NOT_ALWAYS”的解决方案,有趣:)
  • 这很好用。虽然没有必要捕获 Exception - 捕获 Exception 看起来很丑。
【解决方案5】:

关闭/隐藏 Android 软键盘

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

it's working for me i hope it's work for you..

打开安卓软键盘

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }

【讨论】:

    【解决方案6】:

    user942821 隐藏它的答案:

    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    但这也适用于我隐藏它:

    imm.toggleSoftInput(0, 0);
    

    您可能还想尝试:

    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    

    当在第一个参数中使用 '0' 时,有时键盘会在我无法弄清楚如何复制的奇怪情况下在错误的位置打开。我仍在测试最后一个示例,但当我发现更多信息时会更新。

    更多信息请参见toggleSoftInput documentation page

    【讨论】:

    • 好吧,InputMethodManager.SHOW_FORCED 只是一个表示 int 值的枚举。所以它当然可以工作,但这就像你将它设置为 InputMethodManager.RESULT_UNCHANGED_SHOWN 一样,它的值是 0。枚举应该总是在需要的地方使用。见developer.android.com/reference/android/view/inputmethod/…
    • 当然,我明白了,但这不应该放在第二个参数中吗?第一个参数用于 showFlags。 public void toggleSoftInput (int showFlags, int hideFlags) 我觉得这样比较合适:imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, InputMethodManager.RESULT_UNCHANGED_SHOWN);
    • 好点! :) 实际上,没有 Enum Flag 支持 ShowFlags 参数中的 0 值。通过再次阅读文档,我意识到他们指定它可以是 0。对不起,我的错,虽然我不认为没有提供默认 Enum 标志是一个好习惯。
    • 我也一直在研究它,看来您应该在这里使用幻数。我添加了文档链接。
    【解决方案7】:

    这很好用

    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
    

    【讨论】:

    • 解释一下就好了。
    • 并非在所有情况下都有效。我发现这是一个更大的问题,然后它似乎首先捕获了所有边缘情况。
    【解决方案8】:

    你也可以试试

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

    【讨论】:

      【解决方案9】:

      这是一个解决方案,它检查键盘是否可见

          public static void hideKeyboard(Activity activity) {
              if (isKeyboardVisible(activity)) {
                  InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                  imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
              }
          }
      
          public static boolean isKeyboardVisible(Activity activity) {
              ///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device
              Rect r = new Rect();
              View contentView = activity.findViewById(android.R.id.content);
              contentView.getWindowVisibleDisplayFrame(r);
              int screenHeight = contentView.getRootView().getHeight();
      
              int keypadHeight = screenHeight - r.bottom;
      
              return
                      (keypadHeight > screenHeight * 0.15);
          }
      

      【讨论】:

        【解决方案10】:
        InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        

        【讨论】:

          【解决方案11】:

          用于隐藏键盘,

          InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
          

          这里,“mView”可以是屏幕上可见的任何视图

          【讨论】:

            【解决方案12】:

            此代码从onItemClickAutoCompleteTextView 内部隐藏键盘

            public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
                 // whatever your code does
                 InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
                 imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
            }
            

            【讨论】:

              【解决方案13】:

              使用 Kotlin 扩展隐藏键盘

              // Simple, reuseable call anywhere in code
              myView.hideKeyboard()
              
              fun View.hideKeyboard() {
                  val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                  inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
              }
              

              如何显示软键盘

              fun View.showKeyboard() {
                  val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                  inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
              }
              

              相关的进一步主题:

              简化将Done/Next... 操作添加到edittext:read this post

              删除要求永远使用getSystemService:Splitties Library

              【讨论】:

                【解决方案14】:
                  public void hideKeyboard(Activity activity) {
                    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                    //Find the currently focused view, so we can grab the correct window token from it.
                    View view = activity.getCurrentFocus();
                    //If no view currently has focus, create a new one, just so we can grab a window token from it
                    if (view == null) {
                        view = new View(activity);
                    }
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                }
                

                然后打电话到你想要的地方

                 hideKeyboard(MainActivity.this);
                

                【讨论】:

                  【解决方案15】:

                  以编程方式隐藏键盘::

                  fun hideKeypad() {
                      val view = currentFocus
                      if (view != null) {
                          val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                          imm.hideSoftInputFromWindow(view.windowToken, 0)
                      }
                  }
                  

                  【讨论】:

                    【解决方案16】:

                    Kotlin 方式隐藏和显示 Android 软键盘:

                    在 kotlin 中创建一个扩展文件并添加以下代码以隐藏/显示键盘。

                    > fun EditText.showKeyboard(){
                      requestFocus()
                      val insetsController = ViewCompat.getWindowInsetsController(this)
                      insetsController?.show(WindowInsetsCompat.Type.ime())
                    }
                    
                    fun Activity.hideKeyboard(){
                      val insetsController = ViewCompat.getWindowInsetsController(window.decorView)
                      insetsController?.hide(WindowInsetsCompat.Type.ime())
                    }
                    

                    【讨论】:

                      【解决方案17】:
                      private void close() {
                          this.requestHideSelf(0);
                      }
                      

                      这个方法很简单

                      【讨论】:

                      • 呃,你从哪里得到这个方法?
                      猜你喜欢
                      • 2022-12-06
                      • 2014-10-04
                      • 2018-01-01
                      • 1970-01-01
                      • 2011-12-16
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-06-22
                      • 2016-06-15
                      相关资源
                      最近更新 更多