警报对话框 - 单选按钮



我有一个警报对话框,我在上面放了单选按钮,但我有一个问题,每当我单击它们时,它们都会被选中。我想要的只是每当我单击一个单选按钮时,它将选择,当我单击另一个单选按钮时,它将取消选择另一个。我该怎么做?这是我的代码..

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

        alert.setTitle("Standard Deviation");
        alert.setIcon(R.drawable.droid);
        RadioButton one = new RadioButton(this);
        one.setText("one set");
        RadioButton two = new RadioButton(this);
        two.setText("two sets");

        LinearLayout ll=new LinearLayout(Treatment.this);
        ll.setOrientation(LinearLayout.VERTICAL);

        ll.addView(one);
        ll.addView(two);
        alert.setView(ll);

        AlertDialog alertDialog = alert.create();
        alertDialog.show();
将它们

放在RadioGroup中,如下所示:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Standard Deviation");
alert.setIcon(R.drawable.droid);
RadioButton one = new RadioButton(this);
one.setText("one set");
RadioButton two = new RadioButton(this);
two.setText("two sets");

RadioGroup rg=new RadioGroup(Treatment.this);
rg.setOrientation(RadioGroup.VERTICAL);

rg.addView(one);
rg.addView(two);
alert.setView(rg);

AlertDialog alertDialog = alert.create();
alertDialog.show();

我建议您在继续之前阅读有关对话框的文档。自定义对话框和多项选择对话框的示例在您遇到的情形中非常有用。尝试在 xml 中添加一个单选按钮组,其中包含两个单选按钮,作为警报对话框的自定义布局。

最新更新