2009-10-07 16 views
98

Próbuję zastąpić domyślny wygląd ToggleButton. Oto XML, który definiuje ToggleButton:Android: Określ dwa różne obrazy dla togglebutton przy użyciu XML

<ToggleButton android:id="@+id/FollowAndCenterButton" 
     android:layout_width="30px" 
     android:layout_height="30px" 
     android:textOn="" android:textOff="" android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5px" 
     android:layout_marginTop="5px" android:background="@drawable/locate_me"/> 

Teraz mamy dwa 30 x 30 ikony chcemy wykorzystać dla kliknęli/państwa niebędącego kliknięciu. W tej chwili mamy kod, który programowo zmienia ikonę tła w zależności od stanu:

centeredOnLocation.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      if (centeredOnLocation.isChecked()) { 
       centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on)); 
      } else { 
       centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me)); 
      } 
     } 
}); 

Oczywiście szukam lepszego sposobu na zrobienie tego. Próbowałem dokonać wyboru dla obrazu tła, który automatycznie przełącza pomiędzy stanami:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/locate_me" /> <!-- default --> 
<item android:state_checked="true" 
     android:drawable="@drawable/locate_me_on" /> <!-- pressed --> 
<item android:state_checked="false" 
     android:drawable="@drawable/locate_me" /> <!-- unchecked --> 

Ale to nie działa; czytanie ToggleButton API (http://developer.android.com/reference/android/widget/ToggleButton.html), wydaje się, że jedynymi odziedziczone atrybuty XML są

XML Attributes 
Attribute Name Related Method Description 
android:disabledAlpha  The alpha to apply to the indicator when disabled. 
android:textOff   The text for the button when it is not checked. 
android:textOn  The text for the button when it is checked. 

Nie wydaje się być android: state_checked atrybut, mimo klasa ma metodę isChecked() i setChecked().

Czy istnieje sposób, aby zrobić to, co chcę w XML, czy utknąłem z moim niechlujnym obejściem?

+0

Uwaga, jeśli nie używasz tekstu, myślę, że lepiej użyć "CompoundButton". – Timmmm

+1

Zignoruj ​​to; "CompoundButton" jest abstrakcyjny! – Timmmm

Odpowiedz

159

Twój kod jest w porządku. Jednak przycisk przełączania wyświetli pierwszy element w selektorze, który pasuje, więc wartość domyślna powinna nadejść jako ostatnia. Ułóż elementy w następujący sposób, aby upewnić się, że wszystkie zostaną użyte:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on 
    <item android:state_pressed="true" /> //currently pressed turning the toggle off 
    <item android:state_checked="true" /> //not pressed default checked state 
    <item /> //default non-pressed non-checked 
</selector> 
+3

To ma sens; Nigdy nie nawiązałem połączenia między instrukcjami selektora i przełącznika. – I82Much

+0

Zrobiłeś mój dzień ... Miałem problemy z przyciskiem, pole wyboru, a następnie wypróbowałem również przycisk radiowy, w końcu ten wpis był pomocny. Bardzo dziękuję Witalijowi Poloneckiemu i I82Munie –

+0

Twoja rada naprawdę mi pomoże. wielkie dzięki. – anticafe

Powiązane problemy