动态单选按钮控制

时间:2011-01-12 13:06:04

标签: 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");
         }
         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();
         }});   
    }

我遇到的问题是出现单选按钮,用户可以选择任何一个。作为初学者,我确定我没有正确设置单选按钮。用户可以选择所有五个按钮,然后一旦选择,他们也无法取消选中它们。用户应该只能从五个中选择一个选项...我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:16)

您必须将单选按钮添加到RadioGroup,然后将RadioGroup添加到布局

我错过了一些信息,例如提交内容,但您的代码应如下所示:

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();
        }
    });   
}

答案 1 :(得分:4)

您必须在布局文件中创建一个RadioGroup

<TableRow>
    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/radiobuttons">
     </RadioGroup>
</TableRow>

然后以编程方式向其添加按钮:

public void makeRadioButtons(Vector tmpVector, int i,
LinearLayout.LayoutParams lp)
{
    RadioButton rb = new RadioButton(this);
    rb.setText((String) tmpVector.elementAt(i));
    //rg is private member of class which refers to the radio group which you can find by id.
    rg.addView(rb, 0, lp);

}

希望这有帮助。

答案 2 :(得分:3)

你的布局。

    <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");
    }

希望这对你有所帮助。