我如何从 Android activity 中动态创建的单选组中获取值

how can i get values from dynamically created radion groups in my Android activity

我正在从数据库中创建 运行 时间的学生出勤列表,并希望针对每个学生动态创建组中的单选按钮,然后在提交按钮上获取他们的出勤值:我的代码如下所示

        Ll = new LinearLayout(this);
        rg = new RadioGroup(this); //create the RadioGroup
        rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL

      RadioButton  rb1  = new RadioButton(this);
        rb1.setText("P");
        rb1.setId(id);
        rg.addView(rb1); //the RadioButtons are added to the radioGroup instead of the layout
        ///////////////////////////////////////////////
        RadioButton  rb2  = new RadioButton(this);
        rb2.setText("A");
        rb2.setId(id+1);
        rg.addView(rb2); //the RadioButtons are added to the radioGroup instead of the layout
        /////////////////////////////////////

        RadioButton  rb3  = new RadioButton(this);
        rb3.setText("L");
        rb3.setId(id+2);
        rg.addView(rb3); //the RadioButtons are added to the radioGroup instead of the layout


        Ll.addView(rg);//you add the whole RadioGroup to the layout

        tr.addView((View)Ll); // A

如何从 Android activity

中动态创建的单选组中获取值

希望这段代码能帮助您获取选定的单选按钮值

 int selectedId = rg.getCheckedRadioButtonId();
    RadioButton radiochecked = (RadioButton) findViewById(selectedId);
    Toast.makeText(context,
            radiochecked.getText(), Toast.LENGTH_SHORT).show();