Android 安卓关闭键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3553779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:44:53  来源:igfitidea点击:

Android dismiss keyboard

android

提问by Manikandan

How do I dismiss the keyboard when a button is pressed?

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

回答by DeRagan

You want to disable or dismiss a virtual Keyboard?

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

If you want to just dismiss it you can use the following lines of code in your button's on click Event

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

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

回答by user2167877

The solution above doesn't work for all device and moreover it's using EditText as a parameter. This is my solution, just call this simple method:

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

回答by RPM

This is my solution

这是我的解决方案

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

回答by Kandha

you can also use this code on button click event

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

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

回答by Marchy

Here's a Kotlin solution (mixing the various answers in thread)

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

Create an extension function (perhaps in a common ViewHelpers class)

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

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

Then simply consume using:

然后简单地使用:

// from activity
this.dismissKeyboard()

// from fragment
activity.dismissKeyboard()

回答by Andy

The first solution with InputMethodManager worked like a champ for me, the getWindow().setSoftInputMode method did not on android 4.0.3 HTC Amaze.

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

@Ethan Allen, I did not need to make the edit text final. Maybe you are using an EditText inner class that you declared the containing method? You could make the EditText a class variable of the Activity. Or just declare a new EditText inside the inner class / method and use findViewById() again. Also, I didn't find that I needed to know which EditText in the form had focus. I could just pick one arbitrarily and use it. Like so:

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

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

回答by androidmalin

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

回答by Mahmoud Mabrok

This Solution make sure that it hides keyboard also do nothing if it not opened. It uses extension so it can be used from any Context Owner class.

此解决方案确保它隐藏键盘,如果它没有打开,它也不做任何事情。它使用扩展,因此可以从任何上下文所有者类中使用。


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


回答by masterwok

By using the context of the view, we can achieve the desired outcome with the following extension methods in Kotlin:

通过使用视图的上下文,我们可以通过 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
        )

Once these are in place, just call:

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

editTextFoo.dismiss()