Android中的EditText输入背景

时间:2016-07-26 03:36:12

标签: android background android-edittext

我像这样使用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"/>

结果如下: enter image description here

与图像一样,我想在输入时更改文本背景,而不是突出显示。

怎么做?

1 个答案:

答案 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);

        }
    });