【问题标题】:Creating a radiogroups with more than three radio buttons?创建具有三个以上单选按钮的单选组?
【发布时间】:2014-02-27 22:09:30
【问题描述】:

我正在尝试制作一个带有问题的应用程序,每个问题都有 4 个答案选项(多项选择),我使用 4 个单选按钮来获得 4 个答案。有没有办法将它们加入一个单选组,或者我应该单独处理每个单选按钮?一个单选组只有 3 个单选按钮,我可以将单选按钮增加到三个以上吗?如果是,那怎么办?

【问题讨论】:

  • 非常感谢您的帮助

标签: android radio-group


【解决方案1】:

您可以从以下代码中获取每个 RadioButton:

RadioGroup rg = (RadioGroup )findViewById(R.id.radio_group);

RadioButton r1 = (RadioButton) rg.getChildAt(0);
RadioButton r2 = (RadioButton) rg.getChildAt(1);
RadioButton r3 = (RadioButton) rg.getChildAt(2);
RadioButton r4 = (RadioButton) rg.getChildAt(3);

是的,您可以在 Radio Group 中添加超过 3 个单选按钮

<RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RadioButton" />
        </RadioGroup>

【讨论】:

  • 在没有代码的情况下在xml中定义radiogroup中的单选按钮是否足够?
  • 您按照我写的方式定义它,然后在您的活动的 onCreate 中使用 RadioGroup rg = (RadioGroup )findViewById(R.id.radioGroup1);然后你可以从这个单选组中获取每个单选按钮,如下所示 RadioButton r1 = (RadioButton) rg.getChildAt(0); RadioButton r2 = (RadioButton) rg.getChildAt(1); RadioButton r3 = (RadioButton) rg.getChildAt(2); RadioButton r4 = (RadioButton) rg.getChildAt(3);在此之后你可以使用每个单选按钮。
【解决方案2】:

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

final RadioButton[] rb = new RadioButton[4];
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<4; 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); 

编辑:

或者你可以在你的xml中定义radiogroup:

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

然后以编程方式向其添加一个额外的按钮:

 RadioGroup rg = (RadioGroup) findViewById(R.id.radiobuttons);//not this RadioGroup rg = new RadioGroup(this);
 rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<4; i++)
    {
        rb[i]  = new RadioButton(this);
        rg.addView(rb[i]); 
        rb[i].setText("Test");
    }

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2011-08-14
    相关资源
    最近更新 更多