任何人都可以解釋這種外觀差異?
SetColorFilter()作用於背景Drawable,setBackgroundResource()是否設置背景顏色。
在Android 2.3版本中,我安全地使用了SetColorFilter()來改變EditText的背景顏色,因爲通過清除過濾器很容易恢復原始顏色。不需要記住它。現在這兩種方式似乎有區別。Android SetColorFilter()與SetBackgroundResource()/ SetColorBackground()
這是一個EditTextPreference對話框,et是EditText ID。
public void afterTextChanged(Editable s) {
String source = s.toString();
et.removeTextChangedListener(this);
if(!source.matches("^[0-9]+$")) {
et.getBackground().setColorFilter(getResources().getColor(R.color.invalid), Mode.OVERLAY);
et.invalidate();
et.selectAll();
} else {
et.getBackground().clearColorFilter();
et.invalidate();
}
et.addTextChangedListener(this);
}
而這是使用SetBackgroundResource()相同的代碼
public void afterTextChanged(Editable s) {
String source = s.toString();
et.removeTextChangedListener(this);
if(!source.matches("^[0-9]+$")) {
et.setBackgroundResource(R.color.invalid);
et.selectAll();
} else {
et.setBackgroundResource(R.color.valid);
}
et.addTextChangedListener(this);
}
你知道你可以調用'et.setError(「Error message」);'並且它會用一個錯誤圖標標記該edittext。就像另一種選擇。 – dymmeh 2013-02-21 20:13:14
@dymmeh你會把你的評論作爲答案?我試過了,我有一個快照,我可以爲其他任何用戶發佈它。正如你所說有一個圖標。我也非常想擁有紅色背景。 – ilomambo 2013-02-22 05:08:19