2012-03-23 31 views
7

Quiero obtener el color del botón .. No pude obtener el color de la función getbackground que devuelve dibujable. Utilicé getolidcolor que devuelve un valor entero pero es 0 (cero) todo el tiempo ... No entiendo dónde hay problema. quizás no es verdadera función ..cómo obtener el color de fondo del botón en Android?

aquí es mi código androide

  int renk = btn1.getSolidColor(); 

     if(renk== Color.GREEN) 
      Toast.makeText(getApplicationContext(), "green" , 1000).show(); 
     else if(renk== Color.RED) 
      Toast.makeText(getApplicationContext(), "red" , 1000).show(); 
     else if(renk== Color.YELLOW) 
      Toast.makeText(getApplicationContext(), "yellow" , 1000).show(); 
     else 
      Toast.makeText(getApplicationContext(), "unknown", 1000).show(); 

     btn1.setBackgroundColor(Color.YELLOW); 
    renk = btn1.getSolidColor(); 


     if(renk== Color.GREEN) 
      Toast.makeText(getApplicationContext(), "green" , 1000).show(); 
     else if(renk== Color.RED) 
      Toast.makeText(getApplicationContext(), "red" , 1000).show(); 
     else if(renk== Color.YELLOW) 
      Toast.makeText(getApplicationContext(), "yellow" , 1000).show(); 
     else 
      Toast.makeText(getApplicationContext(), "unknown", 1000).show(); 

acabo de recibir mensaje de aviso emergente desconocida, incluso me puse como fondo de color amarillo ..

Respuesta

8

Aquí ya go ....

Button myButton = (Button) findViewById(R.id.takePicture); 

myButton.setBackgroundDrawable(new PaintDrawable(Color.YELLOW)); 

PaintDrawable drawable = (PaintDrawable) myButton.getBackground(); 

int color = drawable.getPaint().getColor(); 
+0

sí Funciona también :) gracias .. – unbalanced

+0

nice solution :) – Meenal

+0

?? ¿No volverá eso siempre AMARILLO? – Matthias

Cuestiones relacionadas