2013-05-27 20 views
0

我試圖比較一個視圖的背景顏色是否與我創建的顏色資源相同,但未找到任何可用的顏色資源。如何檢索視圖的背景顏色並將其與顏色資源進行比較

我很樂意使用任何方法,無論是轉換爲HEX還是String或Int,只要它有效。這是我目前的方法的一個例子。

我設置視圖的用背景色:

ColorDrawable chosenColorBox = (ColorDrawable) chosenColor.getBackground(); 
int colorId = chosenColorBox.getColor(); 

colorId == `-611329` 

使用上R.color.tag_pinkInteger.toString顯示值作爲2130968581這顯然不能:

chosenColor.setBackgroundColor(getResources().getColor(R.color.tag_pink)); 

當我使用這種方法檢索它在if聲明「與檢索值進行比較。

什麼是最佳方式因此我不必對個人價值進行硬編碼,這使我無法調整色彩資源?

+0

!解決方法:colorId = getResources()的getColor(R.color.tag_no_tag_chosen) – papium

回答

2

嘗試比較顏色與Context.getResources().getColor(R.color.tag_pink)而不是資源的ID(這不是顏色!)。返回值代表0xAARRGGBB形式的顏色。

+0

太感謝了,拉茲。有效。我應該意識到我引用的是ID而不是顏色。接受答案和最終解決方案如下。 (colorId!= getResources()。getColor(R.color.tag_no_tag_chosen)) – papium

0

只是簡單的回答:

int viewColor=((ColorDrawable) YOUR_VIEW.getBackground()).getColor(); 

if(viewColor==Color.parseColor("#ffffffff"){ 
    //Color matched 
}