很多时候需要以AlertDialog对话框的形式和用户进行交互,因为对于一些小数据数据信息交互,而且可以不必要的打开新的Activity而累赘。
dialog.getWindow().setContentView(layout)就会出现弹不起软键盘的尴尬。原来,通过阅读官方文档:
Note: Activities provide a facility to manage the creation, saving and restoring of dialogs. See onCreateDialog(int)
, onPrepareDialog(int, Dialog)
, showDialog(int)
, anddismissDialog(int)
. If these methods are used, getOwnerActivity()
will return the Activity that managed this dialog.
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);
android:windowSoftInputMode="adjustPan|stateHidden"