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. The problem is that, if I clicked cancel on this dialog, A is now still being checked. How can I make sure that if the user has pressed cancel, A is remained unchecked? 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);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.