2013-01-14 9 views
11

Mam przyciski niestandardowe, które mają mieć różne tła w zależności od tego, czy są wybrane, czy nie. Chcę wiedzieć, czy istnieje sposób, aby to stwierdzić w pliku XML. Mam przycisk Celsjusza i przycisk Fahrenheita. Chcę, żeby działało tam, gdzie jest wybrane, pozostaje "wciśnięte" i nie można go kliknąć, a drugi przycisk można nacisnąć.Jak ustawić tło wybranego/niezaznaczonego przycisku w pliku XML

 <Button 
      android:id="@+id/celsiusButton" 
      android:text="C" 
      android:background="@drawable/button_unpressed_shape" 
      android:layout_weight="3" 
      android:layout_height="match_parent" 
      android:layout_width="0dip" 
      android:gravity="center" /> 

     <Button 
      android:id="@+id/fahrenheitButton" 
      android:text="F" 
      android:background="@drawable/button_unpressed_shape" 
      android:layout_weight="3" 
      android:layout_height="match_parent" 
      android:layout_width="0dip" 
      android:gravity="center" /> 

Przycisk Celsjusza jest ustawiony domyślnie na wybrany. Staram się pracować nad tym jak to w moim kodu, ale po prostu wydaje się z plątaniną:

tempText = (TextView) findViewById(R.id.temperatureId); 
    celsiusButton = (Button) findViewById(R.id.celsiusButton); 
    celsiusButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed_shape)); 
    celsiusButton.setClickable(false); 

    celsiusButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      if(hasRead) { 
        celsiusButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed_shape)); 
        celsiusButton.setClickable(false); 
        fahrenheitButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_unpressed_shape)); 
        fahrenheitButton.setClickable(true); 
        temperature = (((temperature - 32) * 5)/9); 
        tempText.setText(Double.toString(temperature).substring(0, (Double.toString(temperature).length() - 2)) + " C"); 
      } 
     }  
    }); 

    fahrenheitButton = (Button) findViewById(R.id.fahrenheitButton); 
    fahrenheitButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      if(hasRead) { 
       fahrenheitButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed_shape)); 
       celsiusButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_unpressed_shape)); 
       celsiusButton.setClickable(true); 
       fahrenheitButton.setClickable(false); 
       temperature = ((temperature * 9)/5) + 32; 
       tempText.setText(Double.toString(temperature).substring(0, (Double.toString(temperature).length() - 2)) + "° F"); 
      } 
     } 
    }); 

Odpowiedz

12

Jeśli jest zaznaczony lub nie wybrano należy użyć przycisku przełączania https://developer.android.com/reference/android/widget/ToggleButton.html

zdawać sobie sprawę, że nadal istnieją 4 stany dla tego

je zdefiniować w selektora jak ten

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" /> 
    <item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/> 
    <item android:state_checked="true" android:drawable="@drawable/likeon"/> 
    <item android:drawable="@drawable/likeinitial"/> 
</selector> 

Następnie definiuje go w przycisk tak

android:background="@drawable/like_button" 

Edit

Można było rzeczywiście tylko używać 1 przycisk do użytku. Alternatywnie można użyć 2 przyciski radiowe

https://developer.android.com/reference/android/widget/RadioButton.html

+0

Dzięki, że wygląda naprawdę ładne. – JuiCe

0

Tak, można wybrać Drawable na podstawie stanu View że jest to renderowania.

Jest to dokładny cel typu ciągnienia Selector. Zobacz przykłady i przewodnik tutaj: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Zasadniczo każdy z selektorów item określa, które stany mają określone wartości. Definiuje także, który drawable reprezentuje ten zbiór wartości.

Następnie można ustawić kod stanu na View, np.

celsiusButton.setPressed(true); 

Jest to świetne rozwiązanie w praktyce, ponieważ oddzielasz ustawienia interfejsu od modelu/kontrolera. Utrzymywanie dużych zestawów rysunków jest łatwiejsze, gdy kod nie jest odpowiedzialny za bezpośrednią zmianę interfejsu użytkownika aplikacji.

Przykładem jest mój selektor pracy:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/ic_background_pressed" android:state_pressed="true"/> 
    <item android:drawable="@drawable/ic_background_focused" android:state_focused="true"/> 
    <item android:drawable="@drawable/ic_background_default" /> 

</selector> 

Ten przykład świadczy tło przycisku zależnie od jego stanu.

5

ten służy do zmiany koloru przycisku na prasowane lub koncentruje napisać ten kod w swojej rozciągliwej folderze

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Button Focused--> 
    <item android:state_focused="true" 
      android:state_pressed="false" 
      android:drawable="@drawable/login_hover" 
      /> 
<!-- Button Focused Pressed--> 
    <item android:state_focused="true" 
      android:state_pressed="true" 
      android:drawable="@drawable/login_hover" 
      /> 
<!-- Button Pressed--> 
    <item android:state_focused="false" 
      android:state_pressed="true" 
      android:drawable="@drawable/login_hover" 
      /> 
<!-- Button Default Image--> 
    <item android:drawable="@drawable/login_bg"/> 

</selector 

http://nishantvnair.wordpress.com/2010/10/05/change-color-of-button-on-click-android/

+0

Aby to działało, musisz to zrobić yourButton.setFocusableInTouchMode (true) – kelvincer

2

Aby zmienić obraz tła:

public void onClick(View v) { 
    if(v == ButtonName) { 
    ButtonName.setImageResource(R.drawable.ImageName); 
    } 
} 

Lub przy użyciu pliku XML:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
    android:drawable="@drawable/login_selected" /> <!-- pressed --> 
    <item android:state_focused="true" 
    android:drawable="@drawable/login_mouse_over" /> <!-- focused --> 
    <item android:drawable="@drawable/login" /> <!-- default --> 
</selector> 

W OnClick, po prostu dodaj ten kod:

ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName));