Android development - RadioButton default selection problem in RadioGroup

foreword

We know that when there are several RadioButtons in a RadioGroup (in fact, as long as they are in the same parent layout), there is a mutual exclusion relationship between RadioButtons, that is to say, only one RadioButton can be selected. But what if we need to select a RadioButton by default? 
The solution does not limit whether the RadioButton is dynamically generated or hard-coded in the xml file.

I am a pit, you must skip it

When I was asked to select a RadioButton by default, I smiled contemptuously, a property easily solved. So I jumped into the pit below gorgeously (I believe you jumped in too). 
My project requires me to dynamically generate RadioButton, so I directly determine whether a certain condition is true when generating RadioButton, and then select the target RadioButton.

for (int i = 0; i < 4; i++) {

    //选项点击监听方便举例,我这里只是简单的生成RadioButton
    RadioButton radioButton = new RadioButton(this);
    radioButton.setText(i + "");
    //然后判断是否需要默认选中
    ……
    //开始跳坑
    radioButton.setChecked(true);

    radioGroup.addView(radioButton, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

I'm not wrong, I believe that everyone uses the method setChecked to set the default selection (if the buddy of RadioButton is written to death in the xml file, it must be the configuration attribute checked=”true”). 
But when we run it, we are confused, the default is selected, but when we modify the options, we find that the RadioButton selected by default will not be unselected. It's a bit spartan. 
One of the easiest ideas to think of is that we go through all the RadioButtons, and then click on the radioButton.setChecked(true), and the rest of the radioButton.setChecked(false). But such a diligent approach, lazy I don't want to do it! 
Sloth Sloth Sloth! ! how should I solve this? The ink has been in the ink for so long, and the solution will come out immediately.

I am the way out

Without asking Brother Gu, Du Niang found a solution. Originally, it was necessary to set an id for each RadioButton (the RadioButton defined by xml naturally has it, and the code generated can directly call the radioButton.setId() method). Check the Default button.

for (int i = 0; i < 4; i++) {
    RadioButton radioButton = new RadioButton(this);
    radioButton.setText(i + "");
    //////给每个RadioButton设置Id
    radioButton.setId(i);

    radioGroup.addView(radioButton, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
//假如需要默认选中第一个按钮
radioGroup.check(0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

postscript

There is no postscript, and I am not writing a novel. I am just here to record the solution. I will not find a solution next time I jump into the pit. If it helps you, please give a like or reply when you are free, and I don't mind if you are not free. (*  ̄ 3)(ε ̄ *) What?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605967&siteId=291194637