AlertDialog中的EditText不能弹出软键盘的解决方法

来源:互联网 发布:设置淘宝不付款 编辑:程序博客网 时间:2024/05/10 08:24

在AlertDialog中使用自定义的View,如果View中有EditText,在上面点击,默认是跳不出软键盘的,不是焦点的问题。
解决方法,有两种,一是把AlertDialog换成Dialog,但这么一来,对话框的最外层会多出一个框,顶部还会空几十个DP,当然可以用setBackgroundDrawable(new ColorDrawable(0))把背景设为透明,隐藏掉边框,但是上面空着的几十个DP还在,对话框就不是在屏幕居中了。
代码:

12345
Dialog ad = new Dialog(context);ad.show();Window window = ad.getWindow();window.setBackgroundDrawable(new ColorDrawable(0));  window.setContentView(R.layout.cancel_sos_dialog);

最好的办法是第二种:

12345
AlertDialog ad =  new AlertDialog.Builder(context).create(); ad.setView(ManagerDialogLayout_.build(context,ad));ad.show();Window window = ad.getWindow();window.setContentView(ManagerDialogLayout_.build(context,ad));

在调用show方法前先调用setView(layout),show后再调用window.setContentView(layout),两个Layout布局应该是相同的。
至于原因,暂时不明,但是确实解决了问题,在EditText上点击,可以调出软键盘,输入法了。

0 0
原创粉丝点击