Android 动态单选按钮控制
代码{Android 动态单选按钮控制,android,Android,代码{ private void createRadioButton() { final RadioButton[] rb = new RadioButton[5]; for(int i=0; i<5; i++){ rb[i] = new RadioButton(this); ll.addView(rb[i]); rb[i].setText("Test"); }
private void createRadioButton() {
final RadioButton[] rb = new RadioButton[5];
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
ll.addView(rb[i]);
rb[i].setText("Test");
}
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
ll.removeView(rb[i]);
}
ll.removeView(submit);
Questions();
}});
}
private void createRadioButton(){
最终单选按钮[]rb=新单选按钮[5];
对于(int i=0;i,必须将单选按钮添加到a,然后将放射组添加到布局
我遗漏了一些信息,如提交的内容,但您的代码应该如下所示:
private void createRadioButton() {
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].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
}
ll.removeView(submit);
Questions();
}
});
}
private void createRadioButton(){
最终单选按钮[]rb=新单选按钮[5];
RadioGroup rg=新的RadioGroup(this);//创建RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//或RadioGroup.VERTICAL
对于(int i=0;i,必须在布局文件中创建一个射线组
<TableRow>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radiobuttons">
</RadioGroup>
</TableRow>
希望这对您有所帮助。您的布局
<LinearLayout
android:id="@+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
</RadioGroup>
</LinearLayout>
代码
RadioGroup rg=(RadioGroup)findviewbyd(R.id.RadioGroup);//不是这个RadioGroup rg=新的RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);//或RadioGroup.VERTICAL
对于(int i=0;i我不使用xml,因为我需要动态创建这些按钮,因为应用程序可能会创建1、2或15个按钮。我不确定单选按钮或组目前是如何工作的。我是否将按钮添加到组中,然后显示组?@Usmaan:是,首先将按钮添加到组中,然后显示整个组。参考:@ShahzadImam setId()如何?@spaaarky21:一年前解决了它..thanx.@MariLuna ll是布局,Questions()应该是类中的函数。它们直接取自问题的代码sumbit呢?@maid450
<LinearLayout
android:id="@+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
</RadioGroup>
</LinearLayout>
RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup);//not this RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++)
{
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText("Test");
}