Android关闭键盘


147

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


使EditText Focusable = False可以完成这项工作。您要完全禁用它吗?
st0le

Answers:


325

您要禁用还是关闭虚拟键盘?

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

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

1
这有两个问题。...一个是myEditText需要为Final。其次,我必须知道哪个EditText框具有焦点。有什么解决办法吗?
伊桑·艾伦

78
对于在这里偶然发现的其他任何人,您可以将活动的(您所在的活动或片段getActivity()getCurrentFocus().getWindowToken()用于第一个arg hideSoftInputFromWindow()。另外,如果要在更改活动时尝试使其消失,请在onPause()而不是在onStop()中进行操作。
德雷克·克拉里斯2013年

2
这个答案,加上这里的评论,完全解决了我的问题!
aveschini

9
解雇键盘真是丑陋,丑陋。我希望将来有一种更清洁的方式来做这种简单的事情。
Subby

73

上面的解决方案不适用于所有设备,而且它使用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);
    }
}

3
isAcceptingText()使这个答案比其他人更好
user1506104

好的解决方案...在极少数情况下,当键盘仍在屏幕上时,isAcceptingText()可以返回false,例如,单击EditText以外的文本以选择但不能在同一窗口中进行编辑的文本。
Georgie

29

这是我的解决方案

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);
    }
}

就是说,如果您知道导致键盘显示的视图。
RPM

16

您还可以在按钮单击事件上使用此代码

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

13

这是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()

5

使用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);

3
欢迎使用Stack Overflow!这实际上是评论,而不是答案。有了更多代表,您就可以发表评论
2012年

4
    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();
    }
}

1

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


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)
    }
}



0

通过使用视图的上下文,我们可以使用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()
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.