【问题标题】:how to get background color of button on android?如何在android上获取按钮的背景颜色?
【发布时间】:2012-04-07 15:24:50
【问题描述】:

我想获取按钮的颜色。我无法从返回可绘制对象的 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();

即使我将背景设置为黄色,我也会收到未知的 toast 消息..

【问题讨论】:

    标签: android background-color


    【解决方案1】:

    给你....

     Button myButton = (Button) findViewById(R.id.takePicture);
    
     myButton.setBackgroundDrawable(new PaintDrawable(Color.YELLOW));
    
     PaintDrawable drawable = (PaintDrawable) myButton.getBackground();
    
     int color = drawable.getPaint().getColor();
    

    【讨论】:

    • ??那不是总是返回黄色吗?
    • 我将这个((ColorDrawable) row.getBackground()).getColor() 用作(row.background as ColorDrawable).color,但我遇到了这个错误android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ColorDrawable
    猜你喜欢
    • 2011-12-26
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2017-11-05
    • 1970-01-01
    • 2022-01-02
    • 2015-02-03
    相关资源
    最近更新 更多