如何根据颜色Android studio更改按钮文本

时间:2017-01-20 20:03:48

标签: java android

我似乎无法弄清楚如何根据当前颜色以编程方式更改按钮文本。例如,我想做类似于下面的伪代码的事情:

IF button colour == pink THEN
   set button text to "Pink"
ELSE 
   set button text to "Empty"

1 个答案:

答案 0 :(得分:1)

假设按钮的背景是颜色(例如,不是图像),那么您可以使用以下代码执行此操作:

int color = ((ColorDrawable) button.getBackground()).getColor();
if(color == Color.rgb(pinkRed, pinkGreen, pinkBlue)) {
   button.setText("Pink");
} else {
   button.setText("Empty");
}