誰かがこの外観の違いを説明できますか?
SetColorFilter()は、setBackgroundResource()が背景色を設定するかどうかに関係なく、背景のDrawableに作用します。
Androidバージョン2.3では、フィルターをクリアすることで元の色を復元するのが非常に簡単なので、SetColorFilter()を使用してEditTextの背景色を安全に変更しました。取得して覚えておく必要はありません。現在、2つの方法には違いがあるようです。
これはEditTextPreferenceダイアログボックスであり、etはEditTextIDです。
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);
}