Fragment中出现dialog消失软键盘强制出现的情况,及解决方法
程序员文章站
2022-06-22 17:34:29
使用到的dialog代码 AlertDialog.Builder builder = new AlertDialog.Builder(context); payingDialog = builder.create(); payingDialog.setCancelable(false); payingDialog.show(); View view = LayoutInflater.from(context).inflate(R.layout.dialog_paying, null); T...
使用到的dialog代码
AlertDialog.Builder builder = new AlertDialog.Builder(context);
payingDialog = builder.create();
payingDialog.setCancelable(false);
payingDialog.show();
View view = LayoutInflater.from(context).inflate(R.layout.dialog_paying, null);
TextView tvName=view.findViewById(R.id.dialog_paying_tv_name);
tvName.setText("正在加载中,请稍候...");
payingDialog.getWindow().setContentView(view);
WindowManager.LayoutParams lp= payingDialog.getWindow().getAttributes();
lp.width=dip2px(context,300);//定义宽度
lp.height=dip2px(context,200);//定义高度
payingDialog.getWindow().setAttributes(lp);
payingDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
采取措施1:在dialog消失后添加
//强制隐藏软键盘
hideSoftKeyboard(context,viewList);
/**
* 隐藏软键盘(可用于Activity,Fragment)
*/
public static void hideSoftKeyboard(Context context, List<View> viewList) {
if (viewList == null) return;
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
for (View v : viewList) {
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
可惜,该措施无效
然后去查询Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);的意思
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);的作用是隐藏软键盘
可以隐藏软键盘
但也会导致界面里面所有需要弹出软键盘的控件均无法显示软键盘。
尝试去掉 这句代码后发现这个问题解决了!
本文地址:https://blog.csdn.net/ilovedogys/article/details/110223086