在一个Activity里面弹出一个Dialog,使用AlertDialog,代码如下:
final AlertDialog d = new AlertDialog.Builder(context).create();
Window window = d.getWindow();
window.setContentView(view);
d.show();
Dialog使用自定义布局,里面有Editext,Checbox,运行之后发现点击EditText不能弹出软键盘,然后开始各种找问题!
解决思路
1. 怀疑是ChecBox把焦点给抢了,给Editext设置属性
android:focusable="true"
android:enabled="true"
android:editable="true"
android:focusableInTouchMode="true"
清单文件:
android:windowSoftInputMode="stateVisible|adjustResize"
同时将CheckBox的这些属性设置为false,并没有什么卵用
2. 手动弹出IM
dialog.setOnShowListener(new OnShowListener() {
public void onShow(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}
});
同样没有什么卵用
3.找博客
Often you will want to have a Dialog display on top of the current input method, because there is no reason for it to accept text. You can do this by setting theWindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM window flag (assuming your Dialog takes input focus, as it the default) with the following code:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
这是默认情况下隐藏软键盘的方法,要重新显示软键盘,要执行下面这段代码:
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
代码调用上面的设置之后还是一样,软键盘就是不给弹出了,你妹的!
哥要使出绝招了
在各种折腾无果之后,突然想到之前其他的解决办法:
1.老子不用Dialog了,换PopupWindow,只是想了下,没有尝试!
2.不使用AlertDialog,直接使用Dialog,需要设置一下主题或者背景
3.记得之前AlertDialog的另外一种用法:
final AlertDialog d = new AlertDialog.Builder(context).create();
d.setView(view);
d.show();
直接setView,不用window.setContentView
运行之后发现可以弹出软键盘了! 问题解决
问题是解决了,但是还是不知道window.setContentView不行是什么原因造成的!有空研究一下源码!