Я хотел бы получить цвет EditText, я мог бы установить его с помощью setBackgroundColor, но функция getBackgroundColor отсутствует
я нашел это
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");
но его не работает и рушится
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
Это должно работать для уровня API 11 и выше
ColorDrawable drawable = (ColorDrawable)edtxt.getBackground();
if(drawable.getColor()==(int)Color.GREEN)
System.out.println("It Green");
Если вы хотите, чтобы это было на предыдущих API, я бы предложил использовать Custom 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 green");
Если вы устанавливаете цвет во время выполнения, может быть лучше сохранить какой-то флаг (например, логический), чтобы узнать, каков цвет фона текста редактирования.