Android 在按钮上单击“隐藏键盘”

Android 在按钮上单击“隐藏键盘”,android,Android,我有一个meme creator应用程序,我有两个文本字段和一个按钮,我想当按下按钮时隐藏键盘,这可能吗 public void dismissKeyboard(Activity activity) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (null != activity.getCurre

我有一个meme creator应用程序,我有两个文本字段和一个按钮,我想当按下按钮时隐藏键盘,这可能吗

public void dismissKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null != activity.getCurrentFocus())
            imm.hideSoftInputFromWindow(activity.getCurrentFocus()
                .getApplicationWindowToken(), 0);
    }

活动必须传递到此方法,键盘将被取消。

您想要的应该已经发生了。单击按钮时,焦点将从文本字段变为按钮,因此键盘将自动隐藏。

您可以使用此行隐藏sof键盘

InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);
将其放入onClick(视图)事件中

您需要导入android.view.inputmethod.InputMethodManager

单击按钮时,键盘将隐藏

EditText editText = (EditText)findViewById(R.id.textBox);

InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

inputManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

这应该在按钮点击事件中

Android Kotlin

在按钮上单击隐藏kotlin中的键盘

在你的课堂上这样使用


到目前为止你试过什么?在StackOverflow上,人们通常期望问题在实际代码方面表现出一定的效果。首先,请自己尝试一下,然后将您的代码发布到onClickListener上。添加到onClickListener上,您自己尝试了什么?你有没有试着在谷歌上搜索这个?如果我用谷歌搜索你的标题,你会在第一次点击中得到答案
fun dismissKeyboard(activity: Activity) {
    val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    if (null != activity.currentFocus) imm.hideSoftInputFromWindow(
        activity.currentFocus!!.applicationWindowToken, 0
    )
}
button.setOnClickListener {
    dismissKeyboard(this)
}