问题
I can't get the below dynamically generated RadioButtons to uncheck when I click on other RadioButton in the same RadioGroup.
I have even resorted to writing my own handler that clears the RadioGroup (below), and tried another that make all RadioButtons .setChecked(false) but this still does not clear the RadioButton that I setChecked in PopulateAccessPoints.
Any ideas?
RelativeLayout rlaAccessPoints;
RadioGroup rg;
public void onRadioButtonClicked(View view) {
// Is the button now checked?
RadioButton rd = (RadioButton)view;
rg.clearCheck();
rd.setChecked(true);
}
private void PopulateAccessPoints(List<clsAccessPoint> accessPoints){
rg = new RadioGroup(this);
for (clsAccessPoint acp : accessPoints) {
RadioButton rd = new RadioButton(this);
rd.setText(acp.ID + ": " + acp.Name);
rd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onRadioButtonClicked(v);
}
});
rg.addView(rd);
}
rlaAccessPoints.addView(rg);
for (int i = 0; i <= rg.getChildCount() - 1; i++){
RadioButton rd = new RadioButton(this);
rd = (RadioButton)rg.getChildAt(i);
String rdText = rd.getText().toString();
int colonPos = rdText.indexOf(":");
rdText = rdText.substring(0, colonPos).toString();
if (Settings.AccessPointID.equals(rdText)){
//rg.check(i);
rd.setChecked(true);
}
}
}
EDIT: I posted an answer below with much more concise code; please look at that instead.
回答1:
The problem is that you are doing things twice. If you already have a radio group that handles toggling and deselecting others. You dont operate on the radiobuttons individually.
Please read the RadioGroup documentation and implement the proper listeners
RadioButton rd = new RadioButton(this); rd = (RadioButton)rg.getChildAt(i);
This makes no sense, first you create a button and then reassign it to a child of RG.
RadioButton rd = (RadioButton)rg.getChildAt(i);
Should be the correct form.
you should implement this listener on the radiogroup: http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
and forget about this part
rd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onRadioButtonClicked(v);
}
});
回答2:
The code I added to try to fix this is confusing everyone; sorry.
The original code is:
RelativeLayout rlaAccessPoints;
RadioGroup rg;
private void PopulateAccessPoints(List<clsAccessPoint> accessPoints){
rg = new RadioGroup(this);
for (clsAccessPoint acp : accessPoints) {
RadioButton rd = new RadioButton(this);
rd.setText(acp.ID + ": " + acp.Name);
rg.addView(rd);
if (Settings.AccessPointID.equals(acp.ID)){
rd.setChecked(true);
}
}
rlaAccessPoints.addView(rg);
}
This shows the same incorrect behaviour (the RadioButton that I set programatically not being unset when I click on another in the same RadioGroup).
Please refer to this code instead. This is the only applicable code; there is no custom event handlers etc.
来源:https://stackoverflow.com/questions/25406509/android-dynamically-generated-radio-buttons-not-unchecking-once-setchecked-progr