これは、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");