android - 以编程方式更改 TextInputLayout 强调色

标签 android colors

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

现在我想知道如何以编程方式更改强调色(下划线、突出显示时的 hintTextColor)。 我似乎无法在 TextInputLayout 中找到合适的方法。

有什么建议吗?提前致谢。

最佳答案

恕我直言,InputTextLayout 无法以编程方式更改标签颜色,因为它是由样式设置的。我检查了 InputTextLayout 的源代码并编写了这个 hack helper 方法来创建对私有(private)颜色成员的访问:

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,它设置标签的颜色。

关于android - 以编程方式更改 TextInputLayout 强调色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440811/

相关文章:

java - 如何排除一个类被proguard保留

安卓错误 : How to prevent overgrown APK file During compile process in eclipse?

python - 3D matplotlib : color depending on x axis position

c# - WPF MetroTheme = 图标资源

android - 如何在 Android 中更改 TextInputLayout 的 boxStrokeColor?

javascript - 为字符串中的相同元素着色

java - 如何在android中恢复sms.db中的短信备份

android - Google Android 开发工具在 Linux 主机上定期崩溃...这正常吗?

javascript - 在 webview 中显示 android Assets 图像文件

java - java中循环梯度和非循环梯度的区别