【问题标题】:get the button color inside a function获取函数内的按钮颜色
【发布时间】:2016-04-26 17:16:56
【问题描述】:

我想在函数中获取button的背景color。我想检查颜色的相等性以执行某些操作。我使用PaintDrawer 但它不工作.. 应用程序崩溃... 这是我的功能

void moves(Button bn)
{

    if(flag==1)
    {
       // Toast.makeText(getApplicationContext(),"Exception",Toast.LENGTH_LONG).show();


        if (bn.getText().equals("RED") && plyr==1 && mov==0|| mov==1 && bn.getText().equals("RED"))
        {


            rembr = bn;
            Toast.makeText(getApplicationContext(),"Exception"+rembr.getText(),Toast.LENGTH_LONG).show();
            mov = 1;
        }
        else if (bn.getText().equals("GREEN") && plyr == 2 && mov == 0 || mov==1 && bn.getText().equals("GREEN"))
        {
            Toast.makeText(getApplicationContext(),"Exception GREEN",Toast.LENGTH_LONG).show();

            rembr = bn;
            mov = 1;
        }



        else if (mov == 1 && bn.getText() != "RED" && bn.getText() != "GREEN")
        {
                /*check
                adjsnt(lbl, rembr);
                end check*/

            falsemov=adjsnt(bn, rembr);
            if (falsemov == 1)
            {
                falsemov = 0;

            }
            else
            {
                mov = 0;
                PaintDrawable drawable = (PaintDrawable) rembr.getBackground();

                int temp = drawable.getPaint().getColor();
                bn.setBackgroundColor(temp);
                bn.setText(rembr.getText());

                rembr.setBackgroundColor(Color.LTGRAY);
                if (plyr == 1)
                {
                    plyr = 2;
                    t1.setBackgroundColor(Color.LTGRAY);
                    t2.setBackgroundColor(Color.GREEN);
                }
                else
                {
                    plyr = 1;
                    t2.setBackgroundColor(Color.LTGRAY);
                    t1.setBackgroundColor(Color.RED);
                }
            }
        }
    }
}

任何帮助都将受到高度赞赏。

【问题讨论】:

标签: android


【解决方案1】:

使用 Color Drawable 类代替绘画

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();

int colorId = buttonColor.getColor();

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 2016-08-12
    • 2011-12-26
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    相关资源
    最近更新 更多