2016-01-20 41 views
0

我想獲得函數內的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); 
       } 
      } 
     } 
    } 
} 

任何幫助將非常appretiated。

+1

可能重複[獲取Android中按鈕的背景顏色](http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android) –

回答

1

使用顏色繪製對象類的塗料代替

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

int colorId = buttonColor.getColor();