`

如何防止在误碰触到dialog之外的区域时dialog消失的问题

阅读更多
通常我们定义Dialog时是这么做的:
    AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
				.setTitle("退出系统")
				.setMessage("确定要退出系统吗?")
				.setView(textEntryView)
				.setPositiveButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
					}
				})
				.setNegativeButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						logout();
					}
				}).create();		
		dialog.show();


但是如果这个时候用户不小心点击了Dialog之外的区域,那么该Dialog就会消失。有什么办法可以防止这个问题呢?

通过查找api文档,我发现了一个方法,于是就尝试了一下,果然有效果。
就是在创建好dialog对象之后,调用一下它的方法:
dialog.setCanceledOnTouchOutside(false);

就可以了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics