确定取消对话框
- 创建对话框构建器对象,类似工厂模式
-
AlertDialog.Builder builder = new Builder(this);
- 设置标题和正文
-
builder.setTitle("警告"); builder.setMessage("若练此功,必先自宫");
-
设置确定和取消按钮
builder.setPositiveButton("现在自宫", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "恭喜你自宫成功,现在程序退出", 0).show(); } }); builder.setNegativeButton("下次再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show(); } });
-
使用构建器创建出对话框对象
AlertDialog ad = builder.create(); ad.show();
-
案例代码
public void click1(View v){
AlertDialog.Builder builder = new Builder(this);
//设置图标
builder.setIcon(android.R.drawable.alert_dark_frame);
//设置标题
builder.setTitle("欲练此功必先自宫");
//设置文本
builder.setMessage("李志平,想清楚哦");
//设置确定按钮
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "感谢使用本软件,再见", 0).show();
}
});
//设置取消按钮
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show();
}
});
//使用创建器,生成一个对话框对象
AlertDialog ad = builder.create();
ad.show();
}