租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

Android中alertdialog对话框如何用

发布时间:2023-12-01 20:32:29

Android中alertdialog对话框如何用

在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();

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