1

是否可以从 res-colors.xml 更改颜色:

<color name="transparent_color">#00000000</color>

在运行时?

更改它的原因是,在我扩展的 EditText 上,我将 backgroundresouce 设置为:

        setBackgroundResource(R.drawable.edittext_rounded_corners);

edittext_rounded_corners.xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/edittext_rounded_corners.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="true"><shape>
            <solid android:color="#FF8000" />
            <stroke android:width="2.3dp" android:color="@color/referenceFrame" />
            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_focused="false" android:state_pressed="true"><shape>
            <solid android:color="#FF8000" />
            <stroke android:width="2.3dp" android:color="#FF8000" />
            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_focused="true" android:state_pressed="false"><shape>
            <solid android:color="#FFFFFF" />
            <stroke android:width="2.3dp" android:color="#FF8000" />
            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_focused="false" android:state_pressed="false"><shape>
            <gradient android:angle="270" android:centerColor="#FFFFFF" android:endColor="#FFFFFF" android:startColor="#F2F2F2" />
            <stroke android:width="0.7dp" android:color="#BDBDBD" />
            <corners android:radius="8dp" />
        </shape></item>
    <item android:state_enabled="true"><shape>
            <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp" />
        </shape></item>

</selector>

当我有错误时,我需要一种方法使@Color/referenceFrame 变为红色。已经完成错误处理,只需要改变参考框架的方法

4

1 回答 1

1

创建一个新的可绘制对象(或选择器,您需要的),并将其referenceFrame设置为红色。让我们调用这个文件edittext_error.xml

然后,当您遇到错误时,只需将 EditText 的背景更改为指向新文件:

setBackgroundResource(R.drawable.edittext_error);
于 2012-08-08T07:32:02.640 回答