2012-03-23 29 views
7

Tôi muốn lấy màu của nút .. Tôi không thể lấy màu từ chức năng getbackground mà trả về có thể rút được. Tôi sử dụng getsolidcolor trả về giá trị nguyên nhưng nó là 0 (không) tất cả thời gian .. Tôi không hiểu đâu là vấn đề. có lẽ chức năng không đúng sự thật của nó ..làm thế nào để có được màu nền của nút trên Android?

đây là mã android tôi

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

tôi chỉ nhận được tin nhắn chúc mừng biết thậm chí tôi đặt nền là màu vàng ..

Trả lời

8

Đây ya đi ....

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

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

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

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

có Nó cũng hoạt động :) cảm ơn bạn .. – unbalanced

+0

giải pháp tốt đẹp :) – Meenal

+0

?? Sẽ không phải lúc nào cũng trở về VÀNG? – Matthias

Các vấn đề liên quan