简体   繁体   中英

how to set dynamically created Radio buttons into a RadioGroup?

I have Radio buttons that are created dynamically.

                     LinearLayout linLayRoot = (LinearLayout)dialogView.findViewById(R.id.dialog_layout_root);
         RadioGroup radGp = new RadioGroup(this);
         linLayRoot.addView(radGp);

         for (String dir : dirArray)
         {

                LinearLayout linLayNew = new LinearLayout(this);
                linLayNew.setGravity(0x10);
                RadioButton radBut = new  RadioButton(this); /// <- this button does not work!
                radBut.setText("");
                TextView tv = new TextView(this);
                tv.setText(dir);
                tv.setPadding(10, 0, 20, 0);
                ImageView ivs = new ImageView(this);

                linLayNew.addView(radBut);
                linLayNew.addView(tv);
                linLayNew.addView(ivs);

                radGp.addView(linLayNew);

         }

            RadioButton radBut1 = new  RadioButton(this); /// <- this button works!
            radBut1.setId(11);
            radBut1.setText("a1");
            radGp.addView(radBut1);

            RadioButton radBut2 = new  RadioButton(this); /// <- this button works!
            radBut2.setId(12);
            radBut2.setText("b2");
            radGp.addView(radBut2);

         radGp.setOnCheckedChangeListener( new OnCheckedChangeListener()
         {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Toast.makeText(getApplicationContext(), String.valueOf(checkedId) , Toast.LENGTH_SHORT).show();
            }
         });

But as you can see from the comments above, they don't really work, ie seems that they aren't binded to the radGp... maybe its because they are in a separate linlearlayout?

Thanks!

the RadioGroup has an addView which takes a view as input. from here it seems you can add the LinearLayout as a child. and the RadioGroup is really a LinearLayout.

UPDATE

i checked the source of RadioGroup.java

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
    if (child instanceof RadioButton) {
        final RadioButton button = (RadioButton) child;
        if (button.isChecked()) {
            mProtectFromCheckedChange = true;
            if (mCheckedId != -1) {
                setCheckedStateForView(mCheckedId, false);
            }
            mProtectFromCheckedChange = false;
            setCheckedId(button.getId());
        }
    }

    super.addView(child, index, params);
}

this clearly shows that if we nest the radio then it will not work. so i guess, you have to go manual no other way.

List<RadioButton>添加您的RadioButtons ,然后您可以使用它来检查它们

    mRadioList.get(i).setChecked(true);

你可以像Ovidiu建议的那样将它们添加到List中,但由于它们不在RadioGroup中,除了设置在位置i检查了RadioButton之外,你应该将setChecked(false)设置为所有其他RadioButton。

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.

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