How to use Android dialog boxes?
The Android dialog box can be created and displayed using the AlertDialog class. Below are the basic steps for creating a dialog box using the AlertDialog class.
- Create an AlertDialog.Builder object.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
- Set the title, message content, and buttons for the dialog box.
builder.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点击确定按钮后的操作
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮后的操作
}
});
- Create an AlertDialog object and display it.
AlertDialog dialog = builder.create();
dialog.show();
By following the steps above, you can create a simple AlertDialog box and display it on the screen. In addition to setting the title, message, and buttons, you can also customize the dialog box by adjusting its style, icon, single selection, multiple selection, and other properties to create different types of dialog boxes.