TextInputLayout: Different color for hint label when not focused

前端 未结 1 489
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 07:53

What I want to do:

When using an EditText embedded in a TextInputLayout I want to ...

  1. Set the Color of the label to GREEN when it\'s de-focu
1条回答
  •  离开以前
    2021-02-04 08:35

    I had a similar problem: I needed to implement a text input layout in which the label has different colours for empty (when there is no text entered in the edit text), "filled" and focused state. My main problem was how to differentiate between the empty and the filled state as setting a different colour for the focused state was already easy using selectors. In the end I decided to define a custom "empty text" state and implement my custom text input layout (which extends the normal text input layout).

    Here is some code:

    In res/values/attrs.xml:

    
    
    
    ...
    
        
        
            
        
    
    
    

    The custom text input layout:

    public class EmptyStateTextInputLayout extends TextInputLayout {
        private boolean emptyText = true;
        private static final int[] EMPTY_TEXT_STATE = new int[]{R.attr.state_empty_text};
    
        public EmptyStateTextInputLayout(Context context) {
            super(context);
        }
    
        public EmptyStateTextInputLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public EmptyStateTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected int[] onCreateDrawableState(int extraSpace) {
            int[] state = super.onCreateDrawableState(extraSpace + 1);
            if (emptyText) {
                mergeDrawableStates(state, EMPTY_TEXT_STATE);
            }
            return state;
        }
    
        public void setEmptyTextState(boolean emptyTextState) {
            this.emptyText = emptyTextState;
            refreshDrawableState();
        }
    
        @Override
        public void addView(View child, int index, ViewGroup.LayoutParams params) {
            if (child instanceof EditText) {
                EditText editText = (EditText) child;
                if (!TextUtils.isEmpty(editText.getText())) {
                    setEmptyTextState(false);
                }
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable editable) {
                        if (!TextUtils.isEmpty(editable)) {
                            setEmptyTextState(false);
                        } else {
                            setEmptyTextState(true);
                        }
                    }
                });
            }
            super.addView(child, index, params);
        }
    }
    

    XML selector to set the colour of label in different states (res/color/input_field_floating_label.xml):

    
    
        
        
         
    
    

    Style for the input text layout (in res/values/styles.xml):

    
    

    Theme and style for the edit text (still in res/values/styles.xml):

    
    
    
    

    Usage:

    
    
                
    
    

    I recommend this blog post to get an idea of working with custom states: http://code.neenbedankt.com/example-of-custom-states-in-android-components/

    0 讨论(0)
提交回复
热议问题