更改EditText背景颜色的问题

3

我有一个简单的布局,其中包含一个编辑框。

当我将EditText的背景设置为一种颜色时

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@android:color/white" />

当EditText获得焦点时,布局的背景颜色会发生奇怪的变化!

我需要动态更改背景颜色,但是调用以下方法后仍然出现相同结果:

subject.setBackgroundColor(Color.parseColor(mycolor));

我也尝试了以下方法:

subject.setBackground(new ColorDrawable(Color.parseColor(mycolor)));

结果是相同的。

基本上,我想在运行时更改EditText的背景颜色。


@ASP 我想要动态地改变颜色,因为我从远程服务器获取颜色。 - user1940676
@ASP 颜色未在资源中找到。 - user1940676
你得到的颜色是字符串还是十六进制颜色代码? - ASP
2个回答

2
解决方案是添加一个父布局来容纳背景,并将EditText的背景设置为null。
我不太喜欢这个解决方案,但它确实可行。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"  >

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null" />
</LinearLayout>

0
试这个:
 String myHexColor = "#CC2233";// color u get from webserver
EditText myView = (EditText) findViewById(R.id.myEditText);
myView.setBackGroundColor(Color.pasrsehexString(myHexColor));

方法pasrsehexString(String)对于类型Color未定义,我使用的是parseColor,它实际上可以工作。 - user1940676
我认为这个可以运行:myView.setBackgroundColor(Color.parseColor("#636161")); - ASP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接