AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.dialog, null);
builder.setView(view);
final AlertDialog dialog = builder.create();
dialog.show();
final Window window = dialog.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
在这段代码中,我们首先创建了一个AlertDialog,然后通过setView()方法将布局文件(dialog.xml)加载进AlertDialog中,接着通过调用show()方法来启动Dialog。然后我们获取并清除AlertDialog 的Window对象的FLAG_NOT_FOCUSABLE和FLAG_ALT_FOCUSABLE_IM标志。最后,我们设置Window的软输入法模式为SOFT_INPUT_ADJUST_RESIZE,这会让Dialog在弹出软键盘时自适应大小。
最后,给出dialog.xml布局文件的示例代码: