2012-03-23 5 views
7

Je veux obtenir la couleur du bouton .. Je ne pouvais pas obtenir la couleur de la fonction getbackground qui renvoie drawable. J'ai utilisé getsolidcolor qui renvoie la valeur entière mais étant 0 (zéro) tout le temps .. Je ne comprends pas où est le problème. peut-être sa fonction pas vrai ..comment obtenir la couleur de fond du bouton sur android?

ici est mon code android

  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(); 

Je viens d'obtenir un message de pain grillé inconnu même je tâche de fond comme jaune ..

Répondre

8

Here 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

oui cela fonctionne aussi :) merci .. – unbalanced

+0

belle solution :) – Meenal

+0

?? Est-ce que ça ne reviendra pas toujours JAUNE? – Matthias

Questions connexes