TextInputLayout设置ErrorText和hint字体颜色

应用在登录页面使用了TextInputLayout加TextInputEditText的组合。突然有一天,设计很操蛋的要求要改hint和ErrorText的字体的颜色。由于之前这里不是我负责的,而之前负责的伙计又很随便,颜色直接用默认的,只能自己想办法了+_+

ErrorText

查看TextInputLayout源码:

    /**
     * Sets an error message that will be displayed below our {@link EditText}. If the
     * {@code error} is {@code null}, the error message will be cleared.
     * 

* If the error functionality has not been enabled via {@link #setErrorEnabled(boolean)}, then * it will be automatically enabled if {@code error} is not empty. * * @param error Error message to display, or null to clear * * @see #getError() */ public void setError(@Nullable final CharSequence error) { // Only animate if we're enabled, laid out, and we have a different error message setError(error, ViewCompat.isLaidOut(this) && isEnabled() && (mErrorView == null || !TextUtils.equals(mErrorView.getText(), error))); }

参数是一个CharSequence,对应的mErrorView是一个TextView,那简单,直接用Span即可:

til_pass.error =  SpannableString(reason.message).apply {
                    setSpan(ForegroundColorSpan(R.color.colorPrimary.color()),0,reason.message.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}

hint

设置hint比较麻烦,查看TextInputLayout源码,在构造方法中:

if (a.hasValue(R.styleable.TextInputLayout_android_textColorHint)) {
       mDefaultTextColor = mFocusedTextColor =
                    a.getColorStateList(R.styleable.TextInputLayout_android_textColorHint);
}

也就是说,存在一个android:textColorHint的属性可以用来设置。但是结果很意外,居然不行…

上网查一下,基本上都是让设置主题中的colorAccent颜色,但是这是全局都要用的颜色,这样做不靠谱啊。
继续翻源码,找到TextInputLayout的工具类CollapsingTextHelper中存在这么一段代码:

    void setCollapsedTextAppearance(int resId) {
        TintTypedArray a = TintTypedArray.obtainStyledAttributes(mView.getContext(), resId,
                android.support.v7.appcompat.R.styleable.TextAppearance);
        // 这才是设置hint颜色的关键
        if (a.hasValue(android.support.v7.appcompat.R.styleable.TextAppearance_android_textColor)) {
            mCollapsedTextColor = a.getColorStateList(
                    android.support.v7.appcompat.R.styleable.TextAppearance_android_textColor);
        }
        if (a.hasValue(android.support.v7.appcompat.R.styleable.TextAppearance_android_textSize)) {
            mCollapsedTextSize = a.getDimensionPixelSize(
                    android.support.v7.appcompat.R.styleable.TextAppearance_android_textSize,
                    (int) mCollapsedTextSize);
        }
        mCollapsedShadowColor = a.getInt(
                android.support.v7.appcompat.R.styleable.TextAppearance_android_shadowColor, 0);
        mCollapsedShadowDx = a.getFloat(
                android.support.v7.appcompat.R.styleable.TextAppearance_android_shadowDx, 0);
        mCollapsedShadowDy = a.getFloat(
                android.support.v7.appcompat.R.styleable.TextAppearance_android_shadowDy, 0);
        mCollapsedShadowRadius = a.getFloat(
                android.support.v7.appcompat.R.styleable.TextAppearance_android_shadowRadius, 0);
        a.recycle();

        if (Build.VERSION.SDK_INT >= 16) {
            mCollapsedTypeface = readFontFamilyTypeface(resId);
        }

        recalculate();
    }

调用该方法的地方在TextInputLayout中:

    /**
     * Sets the hint text color, size, style from the specified TextAppearance resource.
     *
     * @attr ref android.support.design.R.styleable#TextInputLayout_hintTextAppearance
     */
    public void setHintTextAppearance(@StyleRes int resId) {
        mCollapsingTextHelper.setCollapsedTextAppearance(resId);
        mFocusedTextColor = mCollapsingTextHelper.getCollapsedTextColor();

        if (mEditText != null) {
            updateLabelState(false);
            // Text size might have changed so update the top margin
            updateInputLayoutMargins();
        }
    }

也就是说可以通过hintTextAppearance属性,设置样式给hint,并且设置颜色通过android:textColor属性来完成,好吧,定义一个style出来:

    <style name="TextInputAppTheme" parent="Base.TextAppearance.AppCompat.Caption">
        <item name="android:textColor">@color/text_third_title_color
    style>

在布局xml中设置app:hintTextAppearance:

            .support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/input_phone_and_email"
                android:textColorHint="@color/input_hint"
                app:hintTextAppearance="@style/TextInputAppTheme"
                app:hintAnimationEnabled="true">

                .support.design.widget.TextInputEditText
                    android:id="@+id/etUserName"
                    style="@style/myEditTextStyle"
                    android:layout_width="match_parent"
                    android:drawablePadding="4dp"
                    android:imeOptions="actionNext"
                    android:textSize="14sp" />

            .support.design.widget.TextInputLayout>

嗯,可以了,演示图就不搞了,太麻烦了~ o( ̄▽ ̄)ブ

你可能感兴趣的:(android)