确定取消按钮
public void click1(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
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();
}
单选对话框
public void click2(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择性别:");
final String[] items = new String[]{
"男",
"女"
};
builder.setSingleChoiceItems(items, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择的是:" + items[which], 0).show();
}
});
AlertDialog ad = builder.create();
ad.show();
}
多选对话框
public void click3(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择你做过的工种:");
final String[] items = new String[]{
"策划",
"导演",
"美工",
"编剧",
"后期",
"宣传"
};
final boolean[] checkedItems = new boolean[]{
false,
false,
false,
false,
false,
false
};
builder.setMultiChoiceItems(items, checkedItems , new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = "";
for(int i = 0; i < 5; i++){
text += checkedItems[i] ? items[i] + "," : "";
}
Toast.makeText(MainActivity.this, "我做过的工种为:" + text, 0).show();
dialog.dismiss();
}
});
builder.show();
}