Как установить фон выбранной / невыбранной кнопки в файле XML
У меня есть пользовательские кнопки, которые должны иметь разный фон в зависимости от того, выбран они или нет. Я хочу знать, есть ли способ заявить об этом в файле XML. У меня есть кнопка для Цельсия и кнопка для Фаренгейта. Я хочу, чтобы он работал, где, если один выбран, он остается "прессованный» и не может быть нажата, в то время как другая кнопка может быть нажата.
Кнопка Цельсия по умолчанию выбрана. Я пытаюсь поработать над этим в своем коде, но это кажется грязным:
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" );
}
}
});