没有选择默认单选按钮

时间:2017-10-23 11:45:18

标签: android dialog alertdialog

我需要一个像这样的对话

enter image description here

但我无法找到执行RadioButton对话框的方法,默认情况下未选择任何选项。我尝试使用null来选择默认按钮,但它不起作用。有什么想法吗?

这是我的代码:

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(titulo)
        .setSingleChoiceItems(items, 0,  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dlgRadiobuttonsListener.seleccionadoRB(DlgRadiobuttons.this, getResources().getStringArray(items)[which]);
            }
        });

    return builder.create();        
}

1 个答案:

答案 0 :(得分:3)

setSingleChoiceItems的第二个参数是要选择的项的索引,传递-1将使android不选择任何人。

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(titulo)
        .setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dlgRadiobuttonsListener.seleccionadoRB(DlgRadiobuttons.this, getResources().getStringArray(items)[which]);
            }
        });

    return builder.create();

}