試試這個..
LinearLayout layout_name = (LinearLayout) findViewById(R.id.layout_name);
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setId(i);//Setting id for the RadioButton
rb[i].setText("Test");
}
rg.check(3);//Checking pirticular RadioButton with id
// 3 is that RadioButton Id name
layout_name.addView(rg);//you add the whole RadioGroup to the layout
編輯:
int selected = rg.getCheckedRadioButtonId();
String type =rb[selected].getText().toString();
Log.d("type", type);
感謝烏拉圭回合的答覆.... :-) – user2833621
@ user2833621檢查我的編輯。 – Hariharan