您的位置:首页 > 其它

解决在自定义AlertDialog中EditText无法弹出软键盘的问题

2016-12-12 21:44 447 查看
原因:今天做一个文件管理器的时候,在文件重命名上遇到了一个问题(准确的说是两个另外一个与本文无关,就不多提)---在弹出的AlertDialog中的EditText居然无法弹出软键盘,我这里需要输入你这是逗我?

多番查询之后知道了原因:

在setContentView()方法之前就调用了show()方法。所以没能够识别(或者是没有这么彻底,具体原因我也不解)。

解决办法:

在show之前,使用 LayoutInflat 来新建一个布局,然后通过setView()方法,先把布局设置进去。后面的就不变。问题就此解决。

alertDialog = new android.app.AlertDialog.Builder(context).create();
alertDialog.setCancelable(true);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)inflater.inflate(contentViewID, null);
alertDialog.setView(layout);
window = alertDialog.getWindow();
window.setContentView(contentViewID);
alertDialog.show();
nameTextView = (EditText)window.findViewById(R.id.dialog_name);
nameTextView.setText(name);
this.name = name;
surebtn = (Button)window.findViewById(R.id.dialog_sureBtn);
canclebtn = (Button)window.findViewById(R.id.dialog_cancleBtn);

这里的LinearLayout是因为自定义的AlertDialog布局的根布局就是LinearLayout,所以,根布局是什么这里就应该用什么。

下面效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息