2012-04-11 2 views
5

Ich habe zwei Radio-Taste in einer Radio-Gruppe. Ich habe auch 2 Androd: Knopf-Checkbox-wenn der Radio-Button abgewählt ist und checkbox_v für wenn der Benutzer das Kontrollkästchen aktiviert. Ich implementierte auch, dass eine Methode onRadioButtonClick, um sicherzustellen, dass nur ein Radio-Button drawable: Kontrollkästchen und der andere hat checkbox_v. Wie kann ich onRadioClick dazu implementieren? irgendeine Idee?Nur eine Radio-Taste zur gleichen Zeit ausgewählt

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; 
      } 
     } 
    } 

Haupt 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

was hast du probiert? fügen Sie hier Ihren Code ein –

+0

fertig. Code hinzugefügt – user182192

Antwort

13

Wenn sie in einem Radiogroup sind, kann nur einer nach dem anderen ausgewählt werden. Wenn beide ausgewählt werden sollen, entfernen Sie sie aus der Funkgruppe.

+10

Falsch. Ich passe den Radioknopf mit meinem eigenen zeichnbaren drawable/checkbox und drawable/checkbox_v anstelle des Standardknopfes an. Zur Zeit kann ich beide Radiotasten auswählen und beide setzen das Bild drawbar/checkbox_v – user182192

0

Ihre Anforderung ist nicht klar. Würde es dir etwas ausmachen, es einfacher zu machen. Ich habe mein Bestes versucht, um Ihre Anforderungen zu verstehen, und das sind die folgenden Ergebnisse :(.

Ein Radio-Button kann Text haben. So müssen Sie nicht ein weiteres TextView in Ihrem Layout hinzufügen. Bitte entfernen Sie sie und fügen Sie eine einfache android:text="blah.. blah.." zu Ihrem Radiobutton.

mit meinem Verständnis haben Sie zwei Zweifel.

Wie Radiobuttons mit benutzerdefinierten Drawables ??here

Kasse die Antwort anpassen.

So deaktivieren Sie den zuvor ausgewählten Radiobutton ??

Die Wahrheit ist, Sie müssen es nicht manuell tun, wie Sie diese Radiobuttons in einer einzigen Radiogruppe halten.

Hoffe, das wird Ihnen helfen.

0

zu aktivieren/deaktivieren Radiobuttons nicht Teil eines Radiogroup, ein Verfahren, in den Android GUI-Designern unter den ‚onClick‘ RadioButton- Eigenschaften hinzufügen (In diesem Beispiel radio1_onClick).

In Ihrem activity.xml unter dem xml für die RadioButton-

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

In Ihrem activity_main.xml würden Sie die folgende Methode schreiben.

3

Da die obere Wahl nicht für mich wie für viele andere funktioniert. Ich teile die Methode, die ich verwendet habe und es funktioniert perfekt.

<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> 

einstellen android:checkedButton="@+id/rb_female" für mich gearbeitet in RadioGroup.