2012-04-11 102 views
5

我在收音機組中有兩個單選按鈕。我還有2個androd:button複選框 - 當單選按鈕被取消選中時,checkbox_v用於選擇複選框。我還實現了一個方法onRadioButtonClick以確保只有一個單選按鈕具有可繪製:複選框,另一個具有checkbox_v。我如何實現onRadioClick來做到這一點?任何想法?一次只能選擇一個單選按鈕

public void onRadioButtonClick(View v) 
{ 
    RadioButton button = (RadioButton) v; 
    boolean checkBox1Selected; 
    boolean checkBox2Selected = false; 

    Toast.makeText(MainActivity.this, 
     button.getId()+ " was chosen."+R.id.wificheckBox, 
     Toast.LENGTH_SHORT).show(); 

    if (button.getId() ==R.id.wificheckBox) { 
     // Toggle status of checkbox selection 
     checkBox1Selected = radiowifiButton.isChecked(); 

     // Ensure that other checkboxes are not selected 
     if (checkBox2Selected) { 
      radiomobileButton.setChecked(false); 
      checkBox2Selected = false; 
     } 
     else if (button.getId() ==R.id.wifimobilecheckBox) { 
      // Toggle status of checkbox selection 
      checkBox2Selected = radiomobileButton.isChecked(); 
      // Ensure that other checkboxes are not selected 
      if (checkBox1Selected) { 
       radiowifiButton.setChecked(false); 
       checkBox1Selected = false; 
      } 
     } 
    } 

主XML

<RadioGroup 
    android:id="@+id/radioGroup" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_1" 
    android:layout_marginLeft="20dp"> 

<LinearLayout 
    android:id="@+id/ll_2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_1" 
    android:layout_marginLeft="20dp" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/wifimobilecheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/button_radio" 
     android:checked="true" 
     android:onClick="onRadioButtonClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/wificheckBox" 
     android:layout_toRightOf="@+id/wificheckBox" 
     android:paddingLeft="15dp" 
     android:text="WiFi or mobile network" 
     android:textColor="#333333" 
     android:textSize="20dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/ll_3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_2" 
    android:paddingTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/wificheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/button_radio" 

     android:onClick="onRadioButtonClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/wificheckBox" 
     android:layout_toRightOf="@+id/wificheckBox" 
     android:paddingLeft="15dp" 
     android:text="WiFi " 
     android:textColor="#333333" 
     android:textSize="20dp" /> 
</LinearLayout> 

</RadioGroup> 

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/> 
+0

你試過了什麼?在這裏粘貼你的代碼 – 2012-04-11 14:07:34

+0

完成。添加代碼 – user182192 2012-04-11 14:09:16

回答

13
如果

它們處於RadioGroup中,只有一個可以同時被選擇。如果你希望能夠被選中,從radiogroup中刪除它們。

+10

錯誤。我用自己的drawable/checkbox和drawable/checkbox_v自定義單選按鈕,而不是標準的。目前,我可以選擇兩個圓形按鈕,都可以設置圖像drawable/checkbox_v – user182192 2012-04-11 14:15:57

0

您的要求不明確。你介意讓它變得更簡單嗎?我盡力瞭解您的要求,並且這些是以下給出的結果:(。

單選按鈕可以有文本,因此您不必在佈局中添加另一個TextView請刪除它們並添加一個簡單的android:text="blah.. blah.."你的單選按鈕。

隨着我的理解,你有兩個疑慮。

如何自定義單選按鈕自定義可繪??

結帳答案here

如何取消選擇之前選定的單選按鈕?

事實是,您不必手動執行此操作,因爲您將這些單選按鈕放到單個radiogroup中。

希望這會幫助你。

0

要檢查/取消選中不是RadioGroup的一部分的單選按鈕,請在Android GUI設計器的 RadioButton Properties'onClick'(在此示例中爲radio1_onClick)下添加一個方法。

在XML下你activity.xml的單選按鈕

RadioButton 
    [blah blah blah] 
    android:id="@+id/radio1" 
    android:onClick="radio1_onClick" 

在你activity_main.xml中,你會寫下面的方法。

private void radio1_onClick(View v) 
{ 
    CompoundButton b=(CompoundButton)findViewById(R.id.radio1); 
    b.setChecked(true); /* light the button */ 

    //b.setChecked(false); /* unlight the button */ 
} 
3

由於最高票數的答案對我來說並不適用於許多其他人。我分享了我使用的方法和完美的工作方式。

<RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checkedButton="@+id/rb_female" 
      android:orientation="horizontal"> 

      <RadioButton 
       android:id="@+id/rb_female" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginEnd="10dp" 
       android:layout_marginRight="10dp" 
       android:button="@drawable/selector_radio_button" 
       android:text="Female" /> 

      <RadioButton 
       android:id="@+id/rb_male" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:button="@drawable/selector_radio_button" 
       android:text="Male" /> 
</RadioGroup> 

設置android:checkedButton="@+id/rb_female"RadioGroup爲我工作。