0
public class MCQSample extends Activity implements OnClickListener{

    TextView title;
    String gotBread;
    RadioGroup AnswerRG;
    int value ;
    int value1;



    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mcqsample); 
        title = (TextView) findViewById(R.id.abc);
        nextP = (Button) findViewById(R.id.nextP);
        backP = (Button) findViewById(R.id.backP);
        AnswerRG = (RadioGroup) findViewById(R.id.AnswerRG);


            Bundle b = getIntent().getExtras();
            value = b.getInt("key", 0);

        }
    }

大家好,我正在做 Android 应用程序并坚持创建动态单选按钮。因为我不知道我需要多少按钮(这取决于值 - 用户输入)。我阅读了一些可以添加到布局中的帖子,但我想添加到 radioGroup 中。有什么办法吗?谢谢

4

2 回答 2

5

步骤#1:RadioButton通过它的构造函数创建一个新的并按照你认为合适的方式配置它。

步骤#2:调用addView()RadioGroup的添加RadioButton到它。

第 3 步:没有第 3 步

于 2012-05-13T12:43:29.427 回答
3

尝试这种方式添加单选按钮RadioGroup

private final int WC = RadioGroup.LayoutParams.WRAP_CONTENT;
RadioGroup.LayoutParams rParams;
AnswerRG = (RadioGroup) findViewById(R.id.AnswerRG);

RadioButton radioButton = new RadioButton(this);
radioButton.setText("Yellow");
radioButton.setId(1001);//set radiobutton id and store it somewhere
rParams = new RadioGroup.LayoutParams(WC, WC);
AnswerRG.addView(radioButton, rParams);
于 2012-05-13T12:47:51.270 回答