示例一: 确认/取消 对话框
/** * 退出游戏确认对话框 */ public void exitGameDialog(String title, String message, String positiveButton, String negativeButton, boolean cancel) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(title); builder.setMessage(message); builder.setPositiveButton(positiveButton, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); MainActivity.this.finish(); } }); if(cancel){//需要显示取消按钮 builder.setNegativeButton(negativeButton, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); } builder.create().show(); }
运行效果