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

发布于 2024-10-11 12:37:38 字数 71 浏览 2 评论 0原文

API 说警报对话框可以有一个、两个或三个按钮,但 SDK 只允许有一个肯定按钮和一个否定按钮。那么我怎样才能添加第三个按钮呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吹泡泡o 2024-10-18 12:37:38

创建对话框时,将如下内容添加到构建器中:

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

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();
小镇女孩 2024-10-18 12:37:38

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

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

        //...

    }});

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

        //...

    }});
も让我眼熟你 2024-10-18 12:37:38

添加任意数量的不带 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();

Add any number of buttons without 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();
埖埖迣鎅 2024-10-18 12:37:38

通过 Jetpack compose M2,您可以使用 AlertDialog按钮 参数:

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")
                }
            }
        }
    )
}

在此处输入图像描述

通过 Compose M3,您可以使用 confirmButton 属性:

  //androidx.compose.material3
  AlertDialog( 
        //..
        confirmButton = {
            Row(
                modifier = Modifier.padding(all = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(onClick = {  }){
                    Text("First")
                }
                TextButton(onClick = { }) {
                    Text("Second")
                }
                TextButton(onClick = {  }) {
                    Text("Third")
                }
            }
        }
    )

通过 Material Components 库您可以使用:

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

在此处输入图像描述

With Jetpack compose M2 you can use the AlertDialog with the buttons parameter:

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")
                }
            }
        }
    )
}

enter image description here

With Compose M3 you can use the confirmButton attribute:

  //androidx.compose.material3
  AlertDialog( 
        //..
        confirmButton = {
            Row(
                modifier = Modifier.padding(all = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(onClick = {  }){
                    Text("First")
                }
                TextButton(onClick = { }) {
                    Text("Second")
                }
                TextButton(onClick = {  }) {
                    Text("Third")
                }
            }
        }
    )

With the Material Components library you can use:

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

enter image description here

メ斷腸人バ 2024-10-18 12:37:38
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文