2012-07-25 83 views
0
<RadioGroup> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/a" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="lolo" 
         /> 

        <RadioButton 
         android:id="@+id/b" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="sh" 
         /> 
       </TableRow> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/c" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="s" 
         /> 

        <RadioButton 
         android:id="@+id/d" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="cannot be determined" 
         /> 
       </TableRow> 
      </RadioGroup> 

我想选择四个中的一个rdio按钮,它像4个单独的单选按钮? ,得到选择的所有R,我在Android的开发利用策略新非常和善引导我感谢名单wny单选按钮组在代码中不能正常工作

回答

1

RadioButton的意见必须是RadioGroup的直接子女,为group-effect工作。

因此,如果您在RadioGroup下添加“TableRow”,则应该在RadioButton上看到的群组性质将不起作用。

对于RadioButton的分组工作,尝试这样的事情。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" > 

    <RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/a" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="lolo" /> 

     <RadioButton 
      android:id="@+id/b" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="sh" /> 

     <RadioButton 
      android:id="@+id/c" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="s" /> 

     <RadioButton 
      android:id="@+id/d" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="cannot be determined" /> 
    </RadioGroup> 

</RelativeLayout> 

如果你这么关心行列结构,然后用自己的布局XML文件本身和执行单选按钮setOnCheckedChangeListener(listener)。当任何单选按钮被点击时,相应的监听器块将被调用,然后取消选中其他单选按钮。

1

我认为你已经使用无线电组,定义该组这是正确的内部单选按钮,而是指该链接Radio Button Group这可能是有用的

1

试试这个:

<RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/a" 
         android:text="lolo" 
         android:button="@drawable/btn_touch"/> 

        <RadioButton 
         android:id="@+id/b" 
         android:text="sh" 
         android:button="@drawable/btn_touch"/> 
       </TableRow> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/c" 
         android:text="s" 
         android:button="@drawable/btn_touch"/> 

        <RadioButton 
         android:id="@+id/d" 
         android:text="cannot be determined" 
         android:button="@drawable/btn_touch"/> 
       </TableRow> 
      </RadioGroup> 

其中btn_touch在绘制文件夹是:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/selected1" 
      android:state_checked="true"/> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/unselected1"/> 
</selector> 
+0

创建一个选中的复选框和另一个未选中的两个图像,它可以帮助你摆脱这个问题。 – AkashG 2012-07-25 10:10:02

+0

错误:错误:找不到与给定名称匹配的资源(在'drawable'处,值为'@ drawable/selected1')。 – dashh 2012-07-25 10:42:50

+0

@ dreams13你必须在你的可绘制文件夹中放入两个名为selected1和unselected1的图像。 – AkashG 2012-07-25 10:46:11

0

也许这可以帮助你,如果你想保持分离行的按钮: 1.从XML删除组,因为你不需要它:在

<TableRow> 
        <RadioButton 
         android:id="@+id/a" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="lolo" /> 

        <RadioButton 
         android:id="@+id/b" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="sh" /> 
       </TableRow> 

       <TableRow> 
        <RadioButton 
         android:id="@+id/c" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="s" /> 

        <RadioButton 
         android:id="@+id/d" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="cannot be determined" /> 
       </TableRow> 

2.使用这个你活动,使其工作:

private RadioButton a; 
    private RadioButton b; 
    private RadioButton c; 
    private RadioButton d; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ... 
     ButtonChange clRadio = new ButtonChange(); 
     a = (RadioButton) findViewById(R.id.a); 
     b = (RadioButton) findViewById(R.id.b); 
     c = (RadioButton) findViewById(R.id.c); 
     d = (RadioButton) findViewById(R.id.d); 
     a.setOnCheckedChangeListener(clRadio); 
     b.setOnCheckedChangeListener(clRadio); 
     c.setOnCheckedChangeListener(clRadio); 
     d.setOnCheckedChangeListener(clRadio); 
     ... 
    } 
    public class ButtonChange implements OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton radio, boolean isChecked) { 
      if(isChecked) 
       switch(radio.getId()) { 
       case R.id.a: 
       case R.id.b: 
       case R.id.c: 
       case R.id.d: 
        a.setChecked(false); 
        b.setChecked(false); 
        c.setChecked(false); 
        d.setChecked(false); 
        radio.setChecked(true); 
        break; 
       default : 
        break; 
       } 
      } 
     } 
    } 

这应该做的伎俩。