Android dynamically generated radio buttons not unchecking once setChecked programmatically

你。 提交于 2020-01-04 13:58:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!