2017-04-09 144 views
0

我有六个RadioButton属于同一个问题,我需要把它们放在RadioGroup中。但是这些RadioButton在某些LinearLayouts中。我试图把一个RadioGroup放到每个RadioButton上,但我不知道我需要为它做些什么。如何把放在RadioGroup里面的LineaLayout中的RadioButton

enter image description here

tab_layout_a.xml

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:layout_gravity="bottom" 
    android:layout_weight="1" 
    android:orientation="vertical"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:orientation="horizontal"> 

          <RadioButton 
           android:id="@+id/tipoPackRadio1" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:layout_weight="1" 
           android:text="Normal" 
           android:textSize="@dimen/radiobuttons_font_sz" /> 

          <RadioButton 
           android:id="@+id/tipoPackRadio2" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:layout_marginLeft="5dp" 
           android:layout_marginRight="5dp" 
           android:layout_weight="1" 
           android:text="Gilda" 
           android:textSize="@dimen/radiobuttons_font_sz" /> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="50dp" 
          android:layout_weight="0.9" 
          android:orientation="horizontal"> 

           <RadioButton 
            android:id="@+id/tipoPackRadio3" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_marginLeft="0dp" 
            android:layout_weight="1" 
            android:text="Aged Pack \n(cheese/salve)" 
            android:textSize="@dimen/radiobuttons_font_sz" /> 

         </LinearLayout> 

        </LinearLayout> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_gravity="bottom" 
         android:layout_weight="1" 
         android:orientation="horizontal"> 


          <RadioButton 
           android:id="@+id/tipoPackRadio4" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:layout_marginRight="0dp" 
           android:layout_weight="1" 
           android:text="Fellowship" 
           android:textSize="@dimen/radiobuttons_font_sz" /> 

          <RadioButton 
           android:id="@+id/tipoPackRadio5" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:layout_marginLeft="5dp" 
           android:layout_marginRight="5dp" 
           android:layout_weight="1" 
           android:text="Fertilizer" 
           android:textSize="@dimen/radiobuttons_font_sz" /> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:layout_weight="0.9" 
          android:orientation="horizontal"> 

           <RadioButton 
            android:id="@+id/tipoPackRadio6" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_marginLeft="0dp" 
            android:layout_weight="1" 
            android:text="Aged Pack \n(honey)" 
            android:textSize="@dimen/radiobuttons_font_sz" 
            android:visibility="visible" /> 

         </LinearLayout> 
        </LinearLayout> 

       </LinearLayout> 

回答

3

尝试这种解决方案..

class MyActivity extends Activity { 

RadioButton _Rone, _Rtwo, _Rthree; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

//after setContentView(.....) 

    _Rone = (RadioButton) findViewById(R.id.radio_one); 
    _Rtwo = (RadioButton) findViewById(R.id.radio_two); 
    _Rthree = (RadioButton) findViewById(R.id.radio_three); 

    _Rone.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      _Rone.setChecked(true); 
      _Rtwo.setChecked(false); 
      _Rthree.setChecked(false); 
     } 
    }); 

    _Rtwo.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      _Rone.setChecked(false); 
      _Rtwo.setChecked(true); 
      _Rthree.setChecked(false); 
     } 
    }); 

    _Rthree.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      _Rone.setChecked(false); 
      _Rtwo.setChecked(false); 
      _Rthree.setChecked(true); 
     } 
    }); 
// do code here 
}` 

通过点击按钮anyradio其他的选择将复位。

+0

我不能这样做,因为我需要LinearLayouts –

+0

试试这个解决方案..它会工作 – deejay

+0

@KeltonHolandaFontenele这段代码可以处理任何类型的布局。它只直接访问RadioButton。 –