在创建AlertDialog时,使用setCancelable(true)方法,允许用户一次点击就可以关闭对话框。示例代码如下:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("这是一个提示对话框"); builder.setCancelable(true); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //点击确定按钮后的操作 } }); AlertDialog dialog = builder.create(); dialog.show();