我像这样使用EditText
:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="input something"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="#04ff00"
android:textColorHint="#0094fd"
android:textColorHighlight="#ffea00"/>
与图像一样,我想在输入时更改文本背景,而不是突出显示。
怎么做?
答案 0 :(得分:1)
使用这个我为你做的.....
final EditText mEditText = (EditText) findViewById(R.id.editText1);
mEditText.setTextColor(Color.GREEN);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
//Lock the listener
mEditText.removeTextChangedListener(this);
String textString = s.toString();
if(textString.length()>0){
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(Color.GRAY), 0, textString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mEditText.setText(spanText);
mEditText.setSelection(textString.length());
}
//Give back the listener
mEditText.addTextChangedListener(this);
}
});