从主题获取EditText的“默认"颜色值 [英] Get an EditText's 'default' color value from theme
本文介绍了从主题获取EditText的“默认"颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Activity
,其中包含3.1上的EditText
.根据用户输入,我更改EditText中文本的颜色(红色表示错误),然后在文本确定时将其重置为黑色.
一个问题与改变活动的整体主题有关.例如,将其从浅色主题更改为常规的深色主题会导致黑色文本在黑色背景上显示-因此,我需要进入并更改代码,而不是在数据正常时将文本重置为白色./p>
如果我将主题更改为活动",则不必更改此代码,我想知道是否可以通过编程方式将给定主题的默认EditText文本颜色拉出,那么我可以将文本切换回默认颜色,而不是白色,黑色等硬编码.
解决方案
根据 解决方案
According to the Theme's docs get the colour directly using 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();
}
这篇关于从主题获取EditText的“默认"颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文