为什么我的RadioButton就像CheckBox一样?

时间:2014-09-03 03:54:48

标签: android

选择一个选项后,当我尝试选择另一个选项时,它不会取消勾选第一个按钮。有人可以帮助我如何只保留一个radiobutton?谢谢

popup4.xml     

<RadioGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android1:id="@+id/radioGroup1"
    android:checkedButton="@+id/radiobutton_m"
    android1:layout_width="fill_parent"
    android1:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android1:layout_alignParentRight="true"
    android:orientation="vertical" >

    <RelativeLayout
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content" >

        <RadioButton
            android1:id="@+id/legs"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:onClick="onClickRadioButton"
            android1:text="@string/legs" />

        <RadioButton
            android1:id="@+id/abs"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignParentTop="true"
            android1:layout_toRightOf="@+id/shoulder"
            android1:onClick="onClickRadioButton"
            android1:text="@string/abs" />

        <RadioButton
            android1:id="@+id/shoulder"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignParentLeft="true"
            android1:layout_below="@+id/legs"
            android1:onClick="onClickRadioButton"
            android1:text="@string/shoulder" />

        <RadioButton
            android1:id="@+id/biceps"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignLeft="@+id/abs"
            android1:layout_below="@+id/abs"
            android1:onClick="onClickRadioButton"
            android1:text="@string/biceps" />

        <RadioButton
            android1:id="@+id/calves"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignRight="@+id/biceps"
            android1:layout_below="@+id/biceps"
            android1:onClick="onClickRadioButton"
            android1:text="@string/calves" />

        <RadioButton
            android1:id="@+id/back"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignParentLeft="true"
            android1:layout_below="@+id/shoulder"
            android1:onClick="onClickRadioButton"
            android1:text="@string/back" />

        <RadioButton
            android1:id="@+id/chest"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignParentLeft="true"
            android1:layout_below="@+id/back"
            android1:onClick="onClickRadioButton"
            android1:text="@string/chest" />

        <RadioButton
            android1:id="@+id/triceps"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:layout_alignBaseline="@+id/chest"
            android1:layout_alignBottom="@+id/chest"
            android1:layout_alignLeft="@+id/calves"
            android1:onClick="onClickRadioButton"
            android1:text="@string/triceps" />

    </RelativeLayout>

 </RadioGroup>

 <Button
    android1:id="@+id/closepopup4"
    android1:layout_width="wrap_content"
    android1:layout_height="wrap_content"
    android1:layout_below="@+id/workoutlists"
    android1:layout_centerHorizontal="true"
    android1:layout_marginTop="40dp"
    android1:text="@string/close" />

<ListView
    android1:id="@+id/workoutlists"
    android1:layout_width="match_parent"
    android1:layout_height="250dp"
    android1:layout_alignParentLeft="true"
    android1:layout_below="@+id/radioGroup1" >
</ListView>

</RelativeLayout>

OnclickRadioButton(这是java文件的一部分)

   public void onClickRadioButton(View v){  
            boolean checked = ((RadioButton) v).isChecked();
            dataHandler d =  new dataHandler(this, null, null, 1);
            switch(v.getId()) {
                case R.id.shoulder:
                    if (checked)
                    s="Shoulder";
                    adapterSomeWorkout(d,s);
                    break;
                case R.id.chest:
                    if (checked)
                    s="Chest";
                    adapterSomeWorkout(d,s);
                    break;
                case R.id.back:
                    if(checked)
                    s="Back";
                    adapterSomeWorkout(d,s);
                    break;
                case R.id.legs:
                    if(checked)
                    s="Legs";
                    adapterSomeWorkout(d,s);
                    break;
                case R.id.biceps:
                    if(checked)
                    s="Biceps";
                    adapterSomeWorkout(d,s);
                    break;
                case R.id.triceps:
                    if(checked)
                    //s="Triceps";
                    //adapterSomeWorkout(d,s);
                    break;
                case R.id.calves:
                    if(checked)
                    s="Calves";
                    adapterSomeWorkout(d,s);
                    break;
                case R.id.abs:
                    if(checked)
                    s="Abs";
                    adapterSomeWorkout(d,s);    
                    break;
            }
   }

1 个答案:

答案 0 :(得分:3)

RadioButtons必须是RadioGroup的直接子项才能维护其所需的属性(即一次只能选择一个属性)。您可以使用android:orientationRadioButtonsRadioGroup扩展LinearLayout对齐。见https://code.google.com/p/android/issues/detail?id=1214