android - 从主题中获取 EditText 的 'default' 颜色值

标签 android android-layout android-theme

我有一个 Activity,其中包含 3.1 上的 EditText。根据用户输入,我更改了 EditText 中文本的颜色(红色表示错误),然后在文本正常时将其重置为黑色。

一个问题与更改 Activity 的整体主题有关。例如,将其从浅色主题更改为常规深色主题会导致黑色文本显示在黑色背景上 - 因此我需要进入并更改代码,而不是在数据正常时将文本重置为白色。

如果我对 Activity 进行主题更改,我不必更改此代码,我想知道是否有办法以编程方式为给定主题提取默认的 EditText 文本颜色,然后我可以将文本切换回默认颜色而不是硬编码的白色、黑色等。

最佳答案

根据Theme's docs使用 obtainStyledAttributes 直接获取颜色。

TypedArray themeArray = context.getTheme().obtainStyledAttributes(new int[] {android.R.attr.editTextColor});
try {
    int index = 0;
    int defaultColourValue = 0;
    int editTextColour = themeArray.getColor(index, defaultColourValue);
}
finally
{
    // Calling recycle() is important. Especially if you use alot of TypedArrays
    // http://stackoverflow.com/a/13805641/8524
    themeArray.recycle();
}

关于android - 从主题中获取 EditText 的 'default' 颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598414/

相关文章:

android - 在 DialogFragment 上设置文本样式

android - 如何在按钮 View 上启用触觉反馈

java - 如何从 Firebase 实时数据库中具有不同键的节点读取相同的值?

android - Gradle 中的 "configuration on demand"到底是什么?

android - 如何撤消 ParseUser.setPassword() 调用?

ios - Flutter 我想把界面设计成这样,大家能帮帮我吗?我正在设计一个关于页面,但我遇到了问题

Android-在特定 Activity 中更改主题

java - 如何在键盘出现时隐藏或保持底部导航向下,同时让文本编辑器工具栏上下移动?

android - ViewCompat.getLayoutDirection 在 onResume() 之前不会返回正确的布局

安卓工作室 0.8.1 : New project and can't preview the L version in the renderer of phones