Dialog类有一个成员变量mShowing用来判断是否可以关闭对话框,mShowing为true表示窗口正打开,可以关闭,mShowing为false表示窗口没显示,不可关闭,我们强制把mShowing设为false,即可达到禁止关闭Dialog的目标,在需要关闭的时候,手动设置为true.
AlertDialog noticeDialog=new AlertDialog.Builder(context).create();
noticeDialog.show();
Window window = noticeDialog.getWindow();
window.setContentView(R.layout.dialog_notice);
TextView notice_text = (TextView)window.findViewById(R.id.notice_text);
notice_text.setText(noticeText);
//利用反射,把mShowing设为true
try {
Field field = noticeDialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(noticeDialog, false);
} catch (Exception e) {
e.printStackTrace();
}