Dialog 无法隐藏软键盘

Dialog 无法隐藏软键盘

Dialog 无法隐藏软键盘

自定义写了一个inputdialog工具类

但是调出软键盘后 再点取消/确定/dialog外 软键盘缺无法一同消失

 Dialog inputDialog = new Dialog(context);
    inputDialog.show();
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.pdms_dialog_input, null);
    inputDialog.getWindow().setContentView(view);
    TextView negative = view.findViewById(R.id.btn_selectNegative);
    TextView positive = view.findViewById(R.id.btn_selectPositive);
    TextView titleText = view.findViewById(R.id.txt_dialog_title);
    ClearEditText editValue = view.findViewById(R.id.txt_input);
    editValue.setInputType(inputType);
    if (!TextUtils.isEmpty(hint)) {
        editValue.setHint(hint);
    }
    if (!TextUtils.isEmpty(value)) {
        editValue.setText(value);
        editValue.setSelection(value.length());
    }
    titleText.setText(title);
    negative.setOnClickListener(v -> inputDialog.dismiss());
    positive.setOnClickListener(v -> {
        String input = editValue.getEditableText() != null
                ? editValue.getEditableText().toString()
                : null;
        if (input != null) {
            consumer.accept(input);
        }
        inputDialog.dismiss();
    });
    
    inputDialog.setOnDismissListener(hideKeyboard());

关于hideKeyboard()网上已经有很多了 无外乎 InputMethodManager的几个藏键盘方法,当然如果你调用并且好用也不会找到这篇文章

后来在网上看到很多方法 要不就是不管用 要不就是要兜个大圈子
最后再stackOverFlow的一个不起眼的回复里看到一条
可以在xml文件里对应的activity里加一句

android:windowSoftInputMode=“stateAlwaysHidden”

如下:

<activity
            android:name=".Name"
            android:label="@string/app_name" 
  	    android:windowSoftInputMode="stateAlwaysHidden">

简单暴力好用,目前没发现有什么大问题 有问题再回来补

发布了4 篇原创文章 · 获赞 1 · 访问量 580

猜你喜欢

转载自blog.csdn.net/qq_42735079/article/details/84794696