获取函数内的按钮颜色

时间:2016-01-20 17:55:42

标签: android

我想在功能中获得button的背景颜色。我想检查颜色是否相等以执行某些操作。我使用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);
                }
            }
        }
    }
}

任何帮助都会非常有用。

1 个答案:

答案 0 :(得分:1)

使用Color Drawable类而不是paint

  

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

     

int colorId = buttonColor.getColor();