Dialog分类
AlertDialog
Dialog 类是对话框的基类,官方建议我们不要直接实例化它,而是使用其子类来获取实例。AlertDialog是系统提供的一个直接子类,它能帮助我们快速构建出不同类型的弹窗。接下来就看下各种类型弹窗的使用。
1、普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher_background)
.setTitle("普通对话框的标题")
.setMessage("我是内容")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
})
//Neutral按钮,显示在dialog的最左面。
.setNeutralButton("不确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了不确定", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
2、列表对话框
String[] itemArray = {"第一个", "第二个", "第三个", "第四个", "第五个"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher_background)
.setTitle("列表对话框的标题")
.setItems(itemArray, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了" + itemArray[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
3、单选对话框
String[] itemArray = {"第一个", "第二个", "第三个", "第四个", "第五个"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher_background)
.setTitle("单选对话框的标题")
.setSingleChoiceItems(itemArray, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
4、多选对话框
String[] itemArray = {"第一个", "第二个", "第三个", "第四个", "第五个"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher_background)
.setTitle("多选对话框的标题")
.setMultiChoiceItems(itemArray, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
5、多选对话框
View customView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_custom_view_1, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher_background)
.setTitle("自定义对话框的标题")
.setView(customView)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
6、DatePickerDialog,日期选择对话框
默认显示
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this,
year + "-" + month + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
}
}, 2024, 5, 4);
datePickerDialog.show();
修改对话框themeStyle为:AlertDialog.THEME_HOLO_LIGHT,效果如下:
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
AlertDialog.THEME_HOLO_LIGHT,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this,
year + "-" + month + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
}
}, 2024, 5, 4);
datePickerDialog.show();
7、TimePickerDialog,时间选择对话框
默认显示
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
}
}, 9, 36, true);
timePickerDialog.show();
修改对话框themeStyle为:AlertDialog.THEME_HOLO_LIGHT,效果如下:
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, AlertDialog.THEME_HOLO_LIGHT, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
}
}, 9, 36, true);
timePickerDialog.show();
8、ProgressDialog
官方已弃用,建议使用ProgressBar代替,自定义Dialog加载对话框。
Android 包含另一个名为 ProgressDialog 的对话框类,该类可显示带有进度条的对话框。此 widget 已废弃,因为它会在显示进度时阻止用户与应用互动。如果您需要指示加载进度或不确定的进度,请遵循进度和 Activity 中的设计准则,并在布局中使用 ProgressBar,而不是 ProgressDialog。
9、DialogFragment
DialogFragment是继承Fragment的,拥有和Fragment一样的生命周期。在使用时,更容易通过生命周期回调来管理弹窗。对于复杂样式的弹窗,使用DialogFragment更加方便和高效。
注:Dialog官方介绍文档