android - Android中的EditText输入背景

标签 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

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

怎么做?

最佳答案

用这个我为你做......

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

        }
    });

关于android - Android中的EditText输入背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38580623/

相关文章:

android - 关于检查应用程序启动 android 更新的新手问题

css - 为什么背景颜色消失了?

ios - 背景音频不会播放

android - 如何知道textcompletion,edittext

android - GET 的最快 URL 以检查 HTTP 可用性?

java - 为什么 findViewById(android.R.id.tabhost) 返回 null?

android - 推送通知是否需要 Google Cloud Messaging Service

css - 固定背景滚动效果

java - 如何在 4.0+ 上改变 Edittext 下划线的颜色?

android - SetNextFocusDownId 行为不正确