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应用程序并坚持创建动态单选按钮。因为我不知道我需要多少按钮(这取决于值 - 用户输入)。我读了一些可以在Layout上添加的帖子,但我想添加到radioGroup中。有什么办法吗?感谢
答案 0 :(得分:5)
步骤1:通过其构造函数创建一个新的RadioButton
并根据需要进行配置。
步骤2:致电addView()
上的RadioGroup
,向其添加RadioButton
。
步骤3:没有第3步
答案 1 :(得分: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);