2012-05-30 4 views
1

에 글고의 배경 색상을 얻기 위해 어떻게 내가 setBackgroundColor하여 설정할 수 있습니다, 글고의 색상을 좀하고 싶습니다,하지만 난이안드로이드

EditText edtxt; 
edtxt.setBackgroundColor(Color.GREEN); 
PaintDrawable drawable; 
Log.d(TAG,"1"); 
drawable = (PaintDrawable)edtxt.getBackground(); 
if(drawable.getPaint().getColor()==(int)Color.GREEN)......... 
Log.d(TAG,"2"); 

하지만 발견 getBackgroundColor 기능

이 아니다 일하는 당신이 편집 텍스트의 배경색 인 알 (예를 들어 부울) 플래그의 어떤 종류를 저장하는 것이 더있을 수 런타임의 색상을 설정하는 경우

05-29 19:20:27.526: E/AndroidRuntime(20255): Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.PaintDrawable 
+0

왜 그렇게 할 것으로 변경됩니다? –

답변

0

충돌하지. 당신이 이전 API를에 냄비하려면

+0

나는 이미 그것에 대해 생각하고 깃발을 선언하기에는 너무 게으르고 코드 톤을 무시할 것이다. – alex6999

4

이것은 내가 정의 EditText을 사용하고 setBackgroundColor(int color) 메서드를 재정의 제안, 최대

ColorDrawable drawable = (ColorDrawable)edtxt.getBackground(); 
if(drawable.getColor()==(int)Color.GREEN) 
System.out.println("It's Green"); 

API 레벨 11에 대한 일을하고 있습니다.

public class NewEditText extends EditText { 
private int color; 
public NewEditText(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

public NewEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
} 

public NewEditText(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
} 


@Override 
public void setBackgroundColor(int color) { 
    // TODO Auto-generated method stub 
    this.color=color; 
    super.setBackgroundColor(color); 
} 

public int getBackgroundColor() { 

    return color; 
} 
} 
레이아웃 사용 지금

:

<com.aneesh.mypackage.NewEditText 
    android:layout_width="fill_parent" 
    android:id="@+id/customview" 
    android:layout_height="wrap_content"/> 

하고 활동 코드

NewEditText custView = (NewEditText)findViewById(R.id.customview); 
custView.setBackgroundColor(Color.GREEN); 
if(custView.getBackgroundColor()==(int)Color.GREEN) 
    System.out.println("It's green"); 
+0

이전에 시도해 봤지만 : getColor() 메서드는 ColorDrawable 형식에 대해 정의되지 않았습니다. – alex6999

+1

아 예,이 메서드는 API 11 (HoneyComb 이상)에서만 사용할 수 있습니다. [getColor()] (http://developer.android.com/reference/android/graphics/drawable/ColorDrawable.html#getColor%28%29) – coderplus

+0

답변을 업데이트 함 :-) – coderplus