Java 以编程方式生成动态RadioGroup,并在其上设置CheckedChangeListener

Java 以编程方式生成动态RadioGroup,并在其上设置CheckedChangeListener,java,android,hashmap,radio-button,android-radiogroup,Java,Android,Hashmap,Radio Button,Android Radiogroup,我制作了下面的代码来显示动态Radiogroup和radiobuttons。 但我无法理解如何将按钮签入setOnCheckedChangeListener()方法。 有时,多个单选按钮将单击同一个放射组。 我不知道怎么做 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll); menuSize = 4; for(i = 0; i < menuSize; i++) { int menuId = cont

我制作了下面的代码来显示动态Radiogroup和radiobuttons。 但我无法理解如何将按钮签入
setOnCheckedChangeListener()
方法。 有时,多个单选按钮将单击同一个放射组。 我不知道怎么做

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);

menuSize = 4;

for(i = 0; i < menuSize; i++)
{
    int menuId = controller.getMenuid(i));
    int subMenuSize = controller.getSubMenu(menuId).size();  // Dynamic value from MVC architechture

    TextView textViewHeading = new TextView(getApplicationContext()); // RadioGroup Heading
    textViewHeading.setText(controller.getMenuName(i));    // Set RadioGroup Heading

    linearLayout.addView(textViewHeading);

    RadioGroup radioGroup = new RadioGroup(getApplicationContext());
    radioGroup.setId(i);

    for(j = 0; j < subMenuSize; j++)
    {
        RadioButton radioButton = new RadioButton(getApplicationContext());

        radioButton.setId(j);

        // Get value from HashMap<Integer, ArrayList<SubMenuClass>>
        // Value is used for RadioButton
        radioButton.setText( controller.getSubMenu(menuId).get(j).getSubMenuName()); 

        radioGroup.addView(radioButton);
    }

    radioGroup.setOnCheckedChangeListener(TryRadioButtons.this);

    linearLayout.addView(radioGroup);
}
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.ll);
菜单化=4;
对于(i=0;i
我想检查按钮提交单击每个放射组都有一个必须检查的RadioButton。 那么,我如何才能在ButtonsSubmitClickEvent中获得RadioButton

THNK提前

这样做

RadioGroup group = new RadioGroup(this); 
group.setOrientation(RadioGroup.HORIZONTAL);
RadioButton btn1 = new RadioButton(this);
btn1.setText("BTN1");
group.addView(btn1);
RadioButton btn2 = new RadioButton(this);
group.addView(btn2);
btn2.setText("BTN2");
.... 
RadioButton btnN = new RadioButton(this);
group.addView(btnN);
btnN.setText("BTNN");
yourLayout.addView(group);


它可能会帮助您动态获取radiobutton

您必须从第一行的线性布局开始,然后循环遍历其子项。我想在setOnCheckedChangeListener()方法中获取radiobutton是的,我知道。我早就知道了。你不必再说了。你对我的建议做了什么?我解决了。在我的代码中有一个问题是setId。我将ID设置为“I”和“j”的值,这两个值可能会在一段时间内保持不变。因此,它将检查同一放射组中的多个单选按钮。顺便说一句,谢谢你的答复。