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");
}
}
});
Dzięki, że wygląda naprawdę ładne. – JuiCe