2017-04-09 17 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。 –

相關問題