如何在对话框关闭时隐藏键盘 [英] How to hide keyboard on dialog dismiss

查看:154
本文介绍了如何在对话框关闭时隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单个FragmentActivity.片段上有一个EditText.

I have an Activity with single Fragment on it. There is one EditText on the fragment.

一旦显示片段,键盘就会弹出,但是我设法在清单android:windowSoftInputMode ="stateHidden"

The keyboard is popping up as soon the fragment is shown, however I managed to block it setting in the manifest android:windowSoftInputMode="stateHidden"

但是,还有一个按钮,可以打开一个带有另一个EditText的对话框.

However, there also is a button, which opens a dialog with another EditText.

我有一种方法可以在关闭对话框时自动关闭键盘.

I have a method that automatically closes the keyboard on dialog dismiss.

public static void closeInput(final View caller) {      
    caller.post(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    });
}

该方法不是一个很好的方法,它存在一些问题.对话框的EditText具有inputType="numberDecimal". closeInput()似乎没有关闭键盘,只是将其更改为默认字母状态.

The method is not a pretty hack and there is something wrong about it. Dialog's EditText has inputType="numberDecimal". The closeInput() seems to be not closing the keyboard, only changing it to the default alphabetical state.

这是怎么回事?

推荐答案

在我的情况下,我使用了方法:

In my case I used method:

public static void closeInput(final View caller) {  
    caller.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }, 100);
}

由于清单中的活动设置,它拒绝正常工作,如果我记得您不能设置android:windowSoftInputMode="any_of_these"

It was refusing to work properly because of activity settings in Manifest, if I recall you can't have android:windowSoftInputMode="any_of_these" set

这篇关于如何在对话框关闭时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆