如何向Android警报对话框添加第三个按钮?

时间:2021-09-27 18:59:42

The API says that the Alert Dialog can have one, two or three buttons, but the SDK only allows for a positive and negative button. How then can I add a third button?

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

4 个解决方案

#1


113  

This code snippet should help explain the three different buttons you can use:

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

    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) {

        //...

    }});

#2


198  

When you are creating the dialog, add something like this to the builder:

在创建对话框时,向构建器添加如下内容:

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

#3


36  

Add any number of buttons without xml:

添加任何数量的没有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();

#4


5  

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

#1


113  

This code snippet should help explain the three different buttons you can use:

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

    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) {

        //...

    }});

#2


198  

When you are creating the dialog, add something like this to the builder:

在创建对话框时,向构建器添加如下内容:

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

#3


36  

Add any number of buttons without xml:

添加任何数量的没有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();

#4


5  

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