1,默认样式

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
2,两个按钮样式

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).setPositiveButton("确定",(dialog,i) ->{
dialog.dismiss();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
3,三个按钮样式

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("确定",(dialog,i) ->{
dialog.dismiss();
})
.setNeutralButton("左侧", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort("左侧");
}
})
//列表样式
// .setItems(item, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// ToastUtils.showShort(item[i]);
// }
// })
;
AlertDialog alertDialog = builder.create();
alertDialog.show();
4,列表选择

private String [] item = {"游戏","运动","电影","旅游","看书","运动","电影","旅游","看书"};
public void alertDialogOne(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
//.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("确定",(dialog,i) ->{
dialog.dismiss();
})
.setNeutralButton("左侧", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort("左侧");
}
})
//列表样式
.setItems(item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort(item[i]);
}
})
;
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
5,单选列表

private String [] item = {"游戏","运动","电影","旅游","看书","运动","电影","旅游","看书"};
public void alertDialogOne(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
//.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("确定",(dialog,i) ->{
dialog.dismiss();
})
.setNeutralButton("左侧", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort("左侧");
}
})
//列表样式
// .setItems(item, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// ToastUtils.showShort(item[i]);
// }
// })
//默认选中第一个
.setSingleChoiceItems(item, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort(item[i]);
}
})
;
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
6,多选列表

private String [] item = {"游戏","运动","电影","旅游","看书","运动","电影","旅游","看书","电影","旅游","看书"};
// 设置boolean数组所有的选项设置默认没选
boolean[] bools = {false,false,false,false,false,false,false,false,false,false,false,false};
public void alertDialogOne(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
//.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("确定",(dialog,i) ->{
dialog.dismiss();
})
.setNeutralButton("左侧", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort("左侧");
}
})
//列表样式
// .setItems(item, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// ToastUtils.showShort(item[i]);
// }
// })
//默认选中第一个
// .setSingleChoiceItems(item, 1, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// ToastUtils.showShort(item[i]);
// }
// })
.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
ToastUtils.showShort(item[i]);
}
})
;
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
7,使用适配器

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,item);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("使用适配器");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
8,自定义样式

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("默认标题")//设置标题
.setIcon(R.mipmap.error_picture)//设置标题图片
.setView(R.layout.dialog_test_one)
//.setMessage("默认文本信息")//设置内容
.setCancelable(false)//设置是否可以点击对话框以外的地方消失
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("确定",(dialog,i) ->{
dialog.dismiss();
})
.setNeutralButton("左侧", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtils.showShort("左侧");
}
})
//列表样式
;
AlertDialog alertDialog = builder.create();
alertDialog.show();
8,设置按钮颜色
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
//都需要在调用AlertDialog的show()方法后进行,否则会报错