简体   繁体   English

Android:RadioButtons中的RadioButtons

[英]Android: RadioButtons within RadioButtons

I have a RadioGroup with 3 radiobuttons, say A, B and C, when I click on A, it will pop up a dialog, and using setSingleChoiceItems of the android dialog, it presents another 4 choices. 我有一个带有3个单选按钮的RadioGroup,分别是A,B和C,当我单击A时,它将弹出一个对话框,并使用android对话框的setSingleChoiceItems表示另外4个选择。 The problem is that, if I clicked cancel on this dialog, A is now still being checked. 问题是,如果我单击此对话框上的“取消”,则仍在检查A。 How can I make sure that if the user has pressed cancel, A is remained unchecked? 我如何确定如果用户按下了取消键,则A未被选中? I tried the following: 我尝试了以下方法:

 CustomizedDialogFragment testDiag = new CustomizedDialogFragment();
           testDiag.show(getFragmentManager(), "Diag");
           if (testDiag.userChecked()){
               radioGroup.check(0);
           } else {
               radioGroup.clearCheck();
           }

    public static class CustomizedDialogFragment extends DialogFragment {
    private boolean checked = false;
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setSingleChoiceItems(R.array.test_values, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0: break;
                            case 1: break;
                            case 2: break;
                            case 3: break;
                        }
                    }
                })

                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        checked = true;
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        checked = false;
                    }
                        });
        return builder.create();
    }

    public boolean userChecked(){
        return checked;
    }

} }

However, this does not work. 但是,这不起作用。 Would really appreciate for anytype of help. 对于任何类型的帮助,我们将不胜感激。

EDIT: added static method to create instance of a fragment 编辑:添加了静态方法来创建片段实例

private RadioGroup mGroup;

public static CustomizedDialogFragment create(RadioGroup group){
    CustomizedDialogFragment instance = new CustomizedDialogFragment(group);
    return instance;
}

protected CustomizedDialogFragment (RadioGroup group){
    mGroup = group;
}

Change negative button click to: 将否定按钮更改为:

setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //Clear RadioGroup selection
                        mGroup.check(-1);
                    }
                        });

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

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