這應該適用於API級別11和高達
ColorDrawable drawable = (ColorDrawable)edtxt.getBackground();
if(drawable.getColor()==(int)Color.GREEN)
System.out.println("It's Green");
如果你想讓它熱鍋上較早的API,我會建議使用自定義EditText
並重寫setBackgroundColor(int color)
方法。
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");
你爲什麼要這麼做? –