简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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