简体   繁体   English

如何使用警报对话框设置检查默认单选按钮android?

[英]how to set check default radio button android with alert dialog?

any body help me... how to set check default radio button with alert dialog when is launched..?任何人帮助我...如何在启动时设置带有警报对话框的检查默认单选按钮..?

this my code , ex: i want to set radio button when launched where items is "15"这是我的代码,例如:我想在启动时设置单选按钮,其中项目为“15”

public void showDialog()
{
    final CharSequence[] items = {"5", "10", "15","20"};

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    alertDialogBuilder.setTitle("Set limit article");

    alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(SettingAppDisplay.this, "You selected item No." + item + ": " + items[item], Toast.LENGTH_SHORT).show();

                if (items[item].equals("5")) {
                    //do what you want
                }
                else if (items[item].equals("10")) {
                    //do what you want                                  
                }
                else if (items[item].equals("15")) {
                    //do what you want
                }
                else if (items[item].equals("20")) {
                    //do what you want
                }

                dialog.dismiss();
            }   
    });
    alertDialogBuilder.show();

}

thanks your paticipation .. sorry with my english :)谢谢你的参与..对不起我的英语:)

Change second argument (checkedItem) in setSingleChoiceItems from -1 to what ever position of radio button you wanted to be checked, here i changed it to '1' so first radio button will be checked.将 setSingleChoiceItems 中的第二个参数( setSingleChoiceItems )从 -1 更改为您想要检查的单选按钮的任何位置,这里我将其更改为“1”,因此将检查第一个单选按钮。

 alertDialogBuilder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(SettingAppDisplay.this, "You selected item No." + item + ": " + items[item], Toast.LENGTH_SHORT).show();

                if (items[item].equals("5")) {
                    //do what you want
                }
                else if (items[item].equals("10")) {
                    //do what you want                                  
                }
                else if (items[item].equals("15")) {
                    //do what you want
                }
                else if (items[item].equals("20")) {
                    //do what you want
                }

                dialog.dismiss();
            }   
    });

See docs查看文档

setSingleChoiceItems (Cursor cursor, int checkedItem, String labelColumn, 
DialogInterface.OnClickListener listener)

Parameters参数

cursor the cursor to retrieve the items from. cursor要从中检索项目的光标。

checkedItem specifies which item is checked. checkedItem指定检查哪个项目。 If -1 no items are checked.如果 -1 则不检查任何项目。

labelColumn The column name on the cursor containing the string to display in the label. labelColumn光标上的列名,包含要在标签中显示的字符串。

listener notified when an item on the list is clicked.单击列表中的项目时通知侦听器。 The dialog will not be dismissed when an item is clicked.单击项目时不会关闭对话框。 It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.只有在单击按钮时才会关闭它,如果没有提供按钮,则由用户决定关闭对话框。

Please check the following android.app.AlertDialog.Builder.setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener) Give the integer value of the position of items as second parameter checkedItem.请检查以下android.app.AlertDialog.Builder.setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener)将项目位置的整数值作为第二个参数checkedItem。

To make default value as 15th item give the following要将默认值设为第 15 项,请执行以下操作

alertDialogBuilder.setSingleChoiceItems(items, 14, new DialogInterface.OnClickListener()

The deafault checked item is set by the middle argument in setSingleChoiceItems默认选中项由setSingleChoiceItems中的中间参数设置

alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

In your code you have that set to -1 which means no item will be selected by default.在您的代码中,您将其设置为 -1,这意味着默认情况下不会选择任何项目。 Just change the value to the one in your array that you want selected.只需将值更改为您想要选择的数组中的值。 Remember to start with 0 for the first one and count your way up to the item you want selected.请记住从第一个 0 开始,然后数到您要选择的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM