【问题标题】:TextInputLayout: Different color for hint label when not focusedTextInputLayout:未聚焦时提示标签的不同颜色
【发布时间】:2016-11-14 14:38:21
【问题描述】:

我想做的事:

当使用嵌入在 TextInputLayout 中的 EditText 时,我想...

  1. 由于用户已经输入了一些值,当标签失焦并漂浮在 EditText 上方时,将标签的颜色设置为绿色
  2. 当标签失焦并位于 EditText 内时,将标签的颜色设置为红色,因为用户尚未输入值
  3. 我不想将所有 EditText 的提示文本颜色更改为 RED,但仅当它们包含在 TextInputLayout 中时(我不需要通用方法 - 一种特定方法,例如为布局 XML 中的每个 TextInputLayout 都可以)
  4. 保留(即不更改)用于在用户聚焦字段时为浮动标签着色的强调色 (YELLOW)。

我尝试过的:

在 TextInputLayout 上将以下设置为主题/样式确实满足 1. 但不满足 2.

<style name="FloatingLabel" parent="Widget.Design.TextInputLayout">
    <item name="android:textColorHint">@color/red</item>
</style>

在嵌入的 EditText 上设置特定颜色,将提示文本更改为另一种颜色:

 android:textColorHint="@color/text_placeholder_gray"

当标签从浮动位置移回 Edittext 作为提示时,实际上会导致提示文本重叠(即没有文本)。

设置:

<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/main_color</item>

在 TextInputLayout 上:

 <android.support.design.widget.TextInputLayout
  ...
   app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" >

更改提示标签的颜色,但它也对焦点状态执行此操作 - 这意味着不满足 4。

而且由于一张图片说的不仅仅是一堆单词(所有字段都处于非聚焦状态):

如何实现满足标准 1-4 的设置?

【问题讨论】:

    标签: android android-layout android-theme android-styles


    【解决方案1】:

    我有一个类似的问题:我需要实现一个文本输入布局,其中标签具有不同颜色的空白(当编辑文本中没有输入文本时)、“填充”和聚焦状态。我的主要问题是如何区分空状态和填充状态,因为使用选择器为焦点状态设置不同的颜色已经很容易了。最后我决定定义一个自定义的“空文本”状态并实现我的自定义文本输入布局(它扩展了正常的文本输入布局)。

    这里有一些代码:

    在 res/values/attrs.xml 中:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
    ...
    
        <!-- Custom state for the text input layout to determine whether the label is shown above some text or not -->
        <declare-styleable name="EmptyTextState">
            <attr name="state_empty_text" format="boolean"/>
        </declare-styleable>
    
    </resources>
    

    自定义文本输入布局:

    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选择器(res/color/input_field_floating_label.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:color="@color/focused_text_color" android:state_focused="true" />
        <item android:color="@color/placeholder_color" app:state_empty_text="true"/>
        <item android:color="@color/primary_text_color"/> <!-- default color -->
    </selector>
    

    输入文本布局的样式(在 res/values/styles.xml 中):

    <style name="EditTextLayout">
        ...
        <item name="android:textColorHint">@color/input_field_floating_label</item>
    </style>
    

    编辑文本的主题和样式(仍在 res/values/styles.xml 中):

    <style name="EditTextTheme">
        ...
        <item name="android:textColorHint">@color/input_field_floating_label</item>
    </style>
    
    <style name="EditText">
        <item name="android:theme">@style/EditTextTheme</item>
        ...
    </style>
    

    用法:

    <com.package.path.widget.EmptyStateTextInputLayout
                style="@style/DarkEditTextLayout"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                ...
                >
    
                <EditText
                    style="@style/EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    </com.package.path.widget.EmptyStateTextInputLayout>
    

    我推荐这篇博文来了解如何使用自定义状态:http://code.neenbedankt.com/example-of-custom-states-in-android-components/

    【讨论】:

    • 当我这样做时,EmptyStateTextInputLayout 结果为空。我错过了什么? (我真的需要这个工作,这是最接近我的解决方案)
    猜你喜欢
    • 2020-03-20
    • 2017-10-16
    • 2018-01-06
    • 2020-02-05
    • 2019-05-24
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多