简体   繁体   English

EditText的可绘制左侧将不会更改

[英]EditText drawable left will not be changed

I have an EditText box that I want to change the background color and drawable left of when the character count is greater than or equal to 4. As seen in my code snippet below I used a TextWatcher to capture typing events and at the moment the background color changes, but the setCompoundDrawable does not change the drawable on the EditText box. 我有一个EditText框,当字符数大于或等于4时,我想更改背景颜色和左侧的可绘制区域。如下面的代码片段所示,我使用了TextWatcher来捕获键入事件,并在此时刻捕获背景颜色会更改,但是setCompoundDrawable不会更改EditText框上的可绘制对象。 Am I doing something wrong, or is it a glitch of some sort? 我是在做错什么,还是某种故障?

final EditText input = (EditText)view.findViewById( R.id.editText );
    input.addTextChangedListener( new TextWatcher() {
        @Override
        public void beforeTextChanged( CharSequence charSequence, int i, int i2, int i3 ) {   }

        @Override
        public void onTextChanged( CharSequence charSequence, int start, int count, int after ) {  }

        @Override
        public void afterTextChanged( Editable editable ) {
                if(editable.length() <= 3 ){
                    input.setBackgroundColor( getResources().getColor( R.color.edittext_background_red) );
                    input.setCompoundDrawables( getResources().getDrawable( R.drawable.ic_cross ), null, null, null );
                }else if(editable.length() >= 4 ){
                    input.setBackgroundColor( getResources().getColor( R.color.edittext_background_green ));
                    input.setCompoundDrawables( getResources().getDrawable( R.drawable.ic_tick ), null, null, null );
                }
        }
    } );

在调用setCompoundDrawables之前,使用setCompoundDrawablesWithIntrinsicBounds(...)或在左侧Drawable上调用setBounds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM