1

我正在使用RadioGroup,添加RadioButton rdbutRadioGroup rdgrp喜欢rdgrp.addView(rdbut)

   for(int j=0;j<3;j++)
   {
         RadioGroup rdgrp = new RadioGroup;
         for(int i=0;i<=10;i++)
         {
             RadioButton rdbut = new RadioButton(this);
             rdbut.setText("RadioButtion"+i);
             rdbut.setId(i);
             rdbut.setTag("somename");
             rdgrp.addView(rdbut);
         }
    }    

上面的代码显示了我如何初始化 radiogroup 和单选按钮。在 emulator/mobile 中运行此代码后,我可以一次检查 2 个单选按钮。

可能是什么问题呢?

4

3 回答 3

1

像这样更改您的代码。

  RadioGroup rdgrp[] = new RadioGroup[3];

  For(int j=0;j<3;j++)
   {
         RadioButton rdbut[] = new RadioButton[10];
         For(int i=0;i<=10;i++)
         {

             rdbut[i].setText("RadioButtion"+i);
             rdbut[i].setId(j*100+i);
             rdbut[i].setTag("somename");
             rdgrp[j].addView(rdbut[i]);
         }
    } 
于 2011-11-25T05:27:19.113 回答
0

您已经创建了三个不同的 Radio Group您只能从单个组中选择一个单选按钮。因此从三个组中您可以选择三个按钮但是没有组间关系。您可以同时从不同组中选择单选按钮。在您的情况下,您最多可以选择三个按钮。

谢谢苏尼尔

于 2011-11-25T05:37:34.540 回答
0

在用户布局文件中使用类似这种 xml 设计的东西。

   <TableLayout
            android:id="@+id/tbl_layoutchoice"
            style="@style/InfoTableView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dip" >

            <RadioGroup
                android:id="@+id/SelectLayout_Group"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </RadioGroup>
 </TableLayout>

和在 Activity 的 OnCreate() 方法中使用这个 RadioGroup 和 findView 像这样

 mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);

然后在需要更改的情况下使用下面的代码在一个 RadioGroup 中添加单选按钮。在下面还需要声明以动态创建单选按钮。

     ArrayList<String> layoutlist = new ArrayList<String>(3);
     int index = -1;
     LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);


   for (String layout : layoutlist) {
        RadioButton r = new RadioButton(this);
        index++;
        r.setText(layout);
        r.setId(index);
        r.setLayoutParams(lp);
        r.setTextAppearance(this, R.style.TextBase);


        mRadioGroup.addView(r);


    }

所以不要忘记在 for 循环之前在 layoutlist 中添加您的字符串值。R.style 是 RadioButton 中文本显示的一些需要样式。

于 2011-11-25T06:00:25.173 回答