【问题标题】:Android dismiss keyboardAndroid 关闭键盘
【发布时间】:2010-08-24 05:25:18
【问题描述】:

按下按钮时如何关闭键盘?

【问题讨论】:

  • 使 EditText Focusable = False 完成这项工作。你想完全禁用它吗?

标签: android


【解决方案1】:

您想禁用或关闭虚拟键盘吗?

如果您只想关闭它,可以在按钮的点击事件中使用以下代码行

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

【讨论】:

  • 这有两个问题....一个是 myEditText 需要是最终的。其次是我必须知道哪个 EditText 框有焦点。有什么解决办法吗?
  • 对于其他在这里绊倒的人,您可以使用活动(您所在的活动或片段getActivity()getCurrentFocus().getWindowToken() 作为hideSoftInputFromWindow() 的第一个参数。此外,如果您想在更改活动时让它消失,请在 onPause() 而不是 onStop() 中执行此操作。
  • 这个答案,结合上面的评论,完全解决了我的问题!
  • 关闭键盘的方法多么丑陋,丑陋。我希望将来有一种更清洁的方式来做这么简单的事情。
【解决方案2】:

上述解决方案不适用于所有设备,而且它使用 EditText 作为参数。这是我的解决方案,只需调用这个简单的方法:

private void hideSoftKeyBoard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

    if(imm.isAcceptingText()) { // verify if the soft keyboard is open                      
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

【讨论】:

  • isAcceptingText() 使这个答案比其他人更好
  • 很好的解决方案...在极少数情况下,当键盘仍在屏幕上时,isAcceptingText() 可能会返回 false,例如从 EditText 处单击到可以选择但不能在同一窗口中编辑的文本.
【解决方案3】:

这是我的解决方案

public static void hideKeyboard(Activity activity) {
    View v = activity.getWindow().getCurrentFocus();
    if (v != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

【讨论】:

  • 如果您知道导致键盘显示的视图。
【解决方案4】:

您也可以在按钮点击事件中使用此代码

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

【讨论】:

    【解决方案5】:

    这是一个 Kotlin 解决方案(在线程中混合各种答案)

    创建一个扩展函数(可能在一个普通的 ViewHelpers 类中)

    fun Activity.dismissKeyboard() {
        val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
        if( inputMethodManager.isAcceptingText )
            inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
    }
    

    然后简单地使用:

    // from activity
    this.dismissKeyboard()
    
    // from fragment
    activity.dismissKeyboard()
    

    【讨论】:

      【解决方案6】:

      第一个使用 InputMethodManager 的解决方案对我来说就像一个冠军,getWindow().setSoftInputMode 方法在 android 4.0.3 HTC Amaze 上没有。

      @Ethan Allen,我不需要将编辑文本定稿。也许您正在使用您声明包含方法的 EditText 内部类?您可以使 EditText 成为 Activity 的类变量。或者只是在内部类/方法中声明一个新的 EditText 并再次使用 findViewById() 。此外,我没有发现我需要知道表单中的哪个 EditText 具有焦点。我可以随意选择一个并使用它。像这样:

          EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);  
          InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
      

      【讨论】:

      【解决方案7】:
          public static void hideSoftInput(Activity activity) {
          try {
              if (activity == null || activity.isFinishing()) return;
              Window window = activity.getWindow();
              if (window == null) return;
              View view = window.getCurrentFocus();
              //give decorView a chance
              if (view == null) view = window.getDecorView();
              if (view == null) return;
      
              InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
              if (imm == null || !imm.isActive()) return;
              imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
          } catch (Throwable e) {
              e.printStackTrace();
          }
      }
      

      【讨论】:

        【解决方案8】:

        此解决方案确保它隐藏键盘如果未打开也不会执行任何操作。 它使用扩展,因此可以从任何上下文所有者类中使用。

        
        fun Context.dismissKeyboard() {
            val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
            val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
            val height = windowHeightMethod.invoke(imm) as Int
            if (height > 0) {
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
            }
        }
        
        
        

        【讨论】:

          【解决方案9】:

          通过使用视图的上下文,我们可以在 Kotlin 中使用以下扩展方法实现预期的结果:

          /**
           * Get the [InputMethodManager] using some [Context].
           */
          fun Context.getInputMethodManager(): InputMethodManager {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                  return getSystemService(InputMethodManager::class.java)
              }
          
              return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
          }
          
          /**
           * Dismiss soft input (keyboard) from the window using a [View] context.
           */
          fun View.dismissKeyboard() = context
                  .getInputMethodManager()
                  .hideSoftInputFromWindow(
                          windowToken
                          , 0
                  )
          

          一旦这些都到位,只需调用:

          editTextFoo.dismiss()
          

          【讨论】:

            猜你喜欢
            • 2023-03-25
            • 2016-03-04
            • 2012-10-12
            • 1970-01-01
            • 2023-03-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多