如何让按钮的背景颜色上的Android? [英] how to get background color of button on android?
本文介绍了如何让按钮的背景颜色上的Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得按钮的颜色..我不可能从函数的getBackground返回绘制得到的颜色。我用getsolidcolor返回整数值,但它为0(零)所有的时间.. 我不知道在哪里的问题。也许它不是真正的功能。
I want to get color of button.. I couldnt get color from getbackground function which returns drawable. I used getsolidcolor which returns integer value but its being 0 (zero) all time.. I dont understand where is problem. maybe its not true function..
这是我的android code
here is my android code
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();
我刚刚得到未知敬酒消息,即使我设置背景为黄色。
I just get unknown toast message even I set background as yellow..
推荐答案
下面亚去......
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();
这篇关于如何让按钮的背景颜色上的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文