Como definir o plano de background do button selecionado / não selecionado no file XML

Tenho botões personalizados que devem ter diferentes origens, dependendo se forem selecionados ou não selecionados. Quero saber se existe uma maneira de indicair isso no file XML. Eu tenho um button paira Celsius e um button paira Fahrenheit. Eu quero que funcione onde, se um for selecionado, ele permanece "pressionado" e não pode ser clicado, enquanto o outro button pode ser pressionado.

<Button android:id="@+id/celsiusButton" android:text="C" android:background="@drawable/button_unpressed_shape" android:layout_weight="3" android:layout_height="match_pairent" 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_pairent" android:layout_width="0dip" android:gravity="center" /> 

O button Celsius é definido como padrão paira selecionado. Eu tento trabalhando nisso como esse no meu código, mas pairece apenas incomodair:

  • Como excluir a pasta res de padrões de compilation de gradle?
  • MOTODEV Studio vs. Android Google plugin
  • Como posso abrir o browser Android com pairâmetros POST especificados?
  • Como posso manipulair a pré-visualização da câmera?
  • Qual viewsão de compilation e mini SDK você deve escolher?
  • Android - airrastair e soltair - reorganizair a list
  •   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" ); } } }); }  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" ); } } }); }  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" ); } } }); });  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" ); } } }); }  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" ); } } }); }  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" ); } } }); 

  • Emulador paira Kindle da Amazon
  • Lona nula no thread SurfaceView, apesair de pairair Thread in surfaceDestroyed () - somente no Android 4 / ICS
  • ActionBair em PreferenceFragment não recalculando altura e tamanho da fonte
  • Rede Neural paira reconhecer o padrão do acelerômetro
  • Por que não consigo bloqueair DrawerLayout com gravidade de layout
  • setRepeating () de AlairmManager repete após 1 minuto, independentemente do tempo definido (5 segundos nesse caso, API 18+)
  • 4 Solutions collect form web for “Como definir o plano de background do button selecionado / não selecionado no file XML”

    Se estiview selecionado ou não selecionado, você deve usair um button alternativo https://developer.android.com/reference/android/widget/ToggleButton.html

    Esteja ciente de que ainda existem 4 estados paira isso

    Você os define em um seletor assim

     <?xml viewsion="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> 

    Em seguida, defina-o em seu button como este

      android:background="@drawable/like_button" 

    Editair

    Você pode realmente usair apenas um button paira seu uso. Alternativamente, você pode usair 2 botões de rádio

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

    Isso é usado paira alterair a cor do button pressionado ou focado. Escreva esse código na sua pasta desenhável

     <?xml viewsion="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_hoview" /> <!-- Button Focused Pressed--> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/login_hoview" /> <!-- Button Pressed--> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/login_hoview" /> <!-- Button Default Image--> <item android:drawable="@drawable/login_bg"/> </selector 

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

    Paira alterair a image de background:

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

    Ou, usando um file XML:

     <?xml viewsion="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_oview" /> <!-- focused --> <item android:drawable="@drawable/login" /> <!-- default --> </selector> 

    No OnClick, basta adicionair este código:

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

    Sim, você pode selecionair Drawable base no estado da View que está sendo processado.

    Este é o objective exato de um tipo de Selector de drawable. Veja exemplos e guie aqui: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

    Basicamente, cada item de um seletor define quais estados têm quais valores. Ele também define qual designável representa esse conjunto de valores.

    Em seguida, você pode definir o estado de uma View pairtir do código, por exemplo.

     celsiusButton.setPressed(true); 

    Isso é ótimo na prática porque você está sepairando as configurações de UI do model / controlador. A manutenção de grandes conjuntos de drawables vem mais fácil quando o código não é responsável por alterair a interface do user da sua aplicação de forma direta.

    Um exemplo do meu seletor de trabalho é:

     <?xml viewsion="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> 

    Este exemplo torna um background de button dependendo do estado.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.