1、普通对话框
// 创建普通对话框
AlertDialog commonDialog = new AlertDialog.Builder(this).setTitle("普通对话框") // 设置标题
.setMessage("是否确定退出") // 设置提示的信息
.setIcon(R.mipmap.ic_launcher) // 设置图标
.setPositiveButton("确定",null) // 添加确定按钮
.setNegativeButton("取消", null) // 添加取消按钮
.create();
commonDialog.show();
运行结果:
2、单选对话框
// 创建单选对话框
AlertDialog singleChoiceDialog = new AlertDialog.Builder(this).setTitle("请选择性别")
.setIcon(R.mipmap.ic_launcher)
.setPositiveButton("确定", null)
.setSingleChoiceItems(new String[]{"男", "女"}, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.create();
singleChoiceDialog.show();
运行结果:
3、多选对话框
/ 创建多选对话框
AlertDialog multiChoiceDialog = new AlertDialog.Builder(this).setTitle("请选择兴趣爱好")
.setIcon(R.mipmap.ic_launcher)
.setPositiveButton("确定", null)
.setMultiChoiceItems(new String[]{"旅游", "美食", "运动", "购物"}, null, null)
.create();
multiChoiceDialog.show();
运行结果:
4、进度条对话框
/ 创建进度条对话框
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("进度条对话框");
progressDialog.setMessage("正在下载,请稍候");
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
运行结果:
5、消息提示框
Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
运行结果: