温馨提示×

Android中alertdialog对话框怎么用

小亿
93
2023-12-01 20:32:29
栏目: 编程语言

在Android中,可以使用AlertDialog来创建对话框。

首先,在代码中创建一个AlertDialog.Builder对象,并设置对话框的标题、消息和按钮等属性:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title") // 设置对话框标题
       .setMessage("Message") // 设置对话框消息
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // 点击确定按钮的逻辑处理
           }
       })
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // 点击取消按钮的逻辑处理
           }
       });

然后,使用builder创建一个AlertDialog对象,并显示对话框:

AlertDialog dialog = builder.create();
dialog.show();

这样就可以创建一个带有标题、消息和确定、取消按钮的对话框。根据需要,可以添加更多的按钮或自定义对话框内容。

0