【问题标题】:How can I add a third button to an Android Alert Dialog?如何将第三个按钮添加到 Android 警报对话框?
【发布时间】:2011-06-07 23:18:11
【问题描述】:

API 说警报对话框可以有一个、两个或三个按钮,但 SDK 只允许一个正面和负面按钮。那么如何添加第三个按钮呢?

【问题讨论】:

标签: android android-alertdialog android-dialog


【解决方案1】:

当您创建对话框时,向构建器添加类似这样的内容:

builder = new AlertDialog.Builder(context);
builder.setTitle("Test");
builder.setIcon(R.drawable.icon);
builder.setMessage("test");
builder.setPositiveButton("Call Now",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });

builder.setNeutralButton("Setup",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                context.startActivity(new Intent(context, Setup.class));
                //dialog.cancel();
            }
        });

builder.setNegativeButton("Exit",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });
builder.create().show();

【讨论】:

  • @ninjasense 是否可以在警报对话框中添加第四个按钮?
  • 在这种情况下 - 没有。您应该制作一个自定义视图。
  • 创建自定义警报对话框更准确。 stackoverflow.com/questions/13341560/…
【解决方案2】:

这段代码 sn-p 应该有助于解释您可以使用的三个不同按钮:

    alertDialog = new AlertDialog.Builder(this).create();

    alertDialog.setTitle("Dialog Button");

    alertDialog.setMessage("This is a three-button dialog!");

    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Button 1 Text", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {

        //...

    } }); 

    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Button 2 Text", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {

        //...

    }}); 

    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Button 3 Text", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {

        //...

    }});

【讨论】:

  • 这已弃用...看看我的回答。
  • @ninjasense,我意识到并且在您写答案时正在编辑。谢谢!
【解决方案3】:

添加任意个不带xml的按钮:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Title");
    builder.setItems(new CharSequence[]
            {"button 1", "button 2", "button 3", "button 4"},
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // The 'which' argument contains the index position
                    // of the selected item
                    switch (which) {
                        case 0:
                            Toast.makeText(context, "clicked 1", 0).show();
                            break;
                        case 1:
                            Toast.makeText(context, "clicked 2", 0).show();
                            break;
                        case 2:
                            Toast.makeText(context, "clicked 3", 0).show();
                            break;
                        case 3:
                            Toast.makeText(context, "clicked 4", 0).show();
                            break;
                    }
                }
            });
    builder.create().show();

【讨论】:

  • 此解决方案不添加按钮,例如“确定”,“取消”等,而是设置要显示在选项列表中的项目,例如“选项1”,“选项2”等。由于问题提到“SDK只允许对于一个积极和消极的按钮”,这并没有回答如何克服这个限制。
  • @d60402 - 你说得很好,但是如果开发人员正在寻找一种方法来创建具有超过 2 个按钮的对话框时遇到这个答案......拥有这个选项非常有用,因为它更多灵活的按钮数量。换句话说,如果正面和负面按钮不足以完成您正在做的事情,那么 3 个按钮可能很快就会变成 4 个。因此投票。
  • @d60402 题目是如何添加第三个按钮,而不是如何显示中性按钮。
  • @Boris Treukhov - 如果对话框有一个肯定按钮和一个否定按钮,这意味着对话框有两个按钮。添加中性按钮将为对话框提供第三个按钮。这基本上就是这个问题的前两个答案所做的。
  • stackoverflow.com/a/19658646/2914140所示,此方案在没有设置对话框消息时会显示按钮。
【解决方案4】:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    this);

            // set title
            alertDialogBuilder.setTitle("To Do List");

            // set dialog message
            alertDialogBuilder
                    .setMessage("What do you want?")
                    .setCancelable(false)
                    .setPositiveButton("Delete All", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close
                            // current activity




                            dialog.cancel();


                        }
                    }).setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, close
                    // current activity




                    dialog.cancel();

                }
            })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing

                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

【讨论】:

  • 这与前 2 个答案有什么区别?
【解决方案5】:

使用 Jetpack compose 1.0.x,您可以将 AlertDialogbuttons 参数一起使用:

val openDialog = remember { mutableStateOf(true) }

if (openDialog.value) {
    AlertDialog(
        onDismissRequest = {
            openDialog.value = false
        },
        title = {
            Text(text = "Title")
        },
        text = {
            Text(
                "Message area"
            )
        },
        buttons = {
            Row(
                modifier = Modifier.padding(all = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(onClick = {  }){
                    Text("First")
                }
                TextButton(onClick = { }) {
                    Text("Second")
                }
                TextButton(onClick = {  }) {
                    Text("Third")
                }
            }
        }
    )
}


通过材料组件库,您可以使用:

MaterialAlertDialogBuilder(context)
        .setTitle(resources.getString(R.string.title))
        .setMessage(resources.getString(R.string.supporting_text))
        .setNeutralButton(resources.getString(R.string.cancel)) { dialog, which ->
            // Respond to neutral button press
        }
        .setNegativeButton(resources.getString(R.string.decline)) { dialog, which ->
            // Respond to negative button press
        }
        .setPositiveButton(resources.getString(R.string.accept)) { dialog, which ->
            // Respond to positive button press
        }
        .show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-23
    • 2017-09-16
    • 2019-11-12
    • 2012-01-03
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多