2017-06-23 59 views
0

我開發了一個應用程序,並且在我的s7和其他設備上一切正常,但我在運行4.4 kitkat的較舊的應用程序上進行了測試,當應用程序比較按鈕顏色時,4.4 ,它不起作用。Android比較ConstantState不適用於4.4 KitKat

這是一段代碼,我使用的是:

for (Button btn : selectorArrayList) { 

     final Button button = btn; 

     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       /* 

        Check if button color is white or red 
        if white, turn red and activate the 
        selector (category) and if it's red 
        turn white and deactivate the selector 
        category) 

       */ 

       // I've set the background in the XML Layout as R.color.white 

       Drawable pd = (Drawable) button.getBackground(); 

       if (pd.getConstantState().equals(ContextCompat.getDrawable(getContext(), R.color.white).getConstantState())) { 

       ... 

在對操作系統的較新版本非常一點上決定了美國是平等的,並在4.4(沒試過其他版本)他們不是。

有沒有人知道我在這裏做錯了什麼?謝謝。

+1

嘗試使用ColorDrawable得到白色 –

+0

它的工作比較像這樣的:if((pd.getColor()0XFFFFFF)== 0XFFFFFF),你知道爲什麼它不以另一種方式工作的原因? – DomeWTF

+1

您在ContextCompat.getDrawable方法中傳遞color res,也許舊版本無法正確處理它。 –

回答

0

試試這個,應該工作:

if (pd.getConstantState().equals(new ColorDrawable(R.color.white).getConstantState())) 
相關問題