以编程方式更改TextInputLayout强调颜色 [英] Change TextInputLayout accent color programmatically

查看:284
本文介绍了以编程方式更改TextInputLayout强调颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的TextInputLayout包含一个EditText视图。

I've got a simple TextInputLayout containing an EditText View.

现在我想知道如何改变口音颜色(下划线,hintTextColor时高亮)
我在TextInputLayout中找不到合适的方法。

Now I wonder how to change the accent color (underline, hintTextColor when highlighted) programmatically. I can't seem to find a suitable method inside TextInputLayout.

有任何建议吗?提前感谢。

Any suggestions? Thanks in advance.

推荐答案

IMHO InputTextLayout不能以编程方式更改标签颜色,因为它是按照样式设置的。我检查了InputTextLayout的源代码并编写了这个创建访问私有颜色成员的Hack帮助方法:

IMHO InputTextLayout can not change label color programmatically, because it is set by style. I examined source code of InputTextLayout and wrote this hack helper method which create access to private color member:

public static void setInputTextLayoutColor(EditText editText, @ColorInt int color) {
    TextInputLayout til = (TextInputLayout) editText.getParent();
    try {
        Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
        fDefaultTextColor.setAccessible(true);
        fDefaultTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{ color }));

        Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
        fFocusedTextColor.setAccessible(true);
        fFocusedTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

mFocusedTextColor用于设置内部CollapsingTextHelper.mCollapsedTextColor

mFocusedTextColor is used for set internal CollapsingTextHelper.mCollapsedTextColor which sets color of label.

这篇关于以编程方式更改TextInputLayout强调颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆