如何在android上获得按钮的背景颜色?

时间:2012-03-23 01:16:35

标签: android background-color

我想得到按钮的颜色..我无法从返回drawable的getbackground函数中获取颜色。我使用了getsolidcolor返回整数值,但它一直是0(零).. 我不明白问题出在哪里。也许它不是真正的功能..

这是我的安卓代码

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

即使我将背景设置为黄色,我也会得到未知的吐司信息。

1 个答案:

答案 0 :(得分:8)

这里你去......

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

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

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

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