无法更改复选框鉴于背景颜色两次 - Android电子 [英] Can't change background color of CheckBox view twice - Android

查看:144
本文介绍了无法更改复选框鉴于背景颜色两次 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变复选框用户后,背景改变其状态为检查。 code以下无法正常工作得很好。如果我点击选中复选框其改变状态,颜色为蓝色,当我再次单击它并取消选中,颜色保持蓝色,而不是变为红色。我也介绍了无效()拨打无sucess。我证实,方法recievies点击和正确地看到检查的地位,但 if语句我所做的似乎只有一次工作。

I try to change CheckBox background after user change its state to Checked. Code below doesn't work quite well. If i click unchecked checkbox its changes state and color to blue, when i click it again and uncheck, color stays blue instead changing to red. I did introduced call of invalidate() with no sucess. I confirmed that method recievies clicks and correctly sees checked status, but if statement i've made seems to work only once.

XML

<CheckBox
    android:id="@+id/chkLargeIcons"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:onClick="handleCheckBoxClick" />

code

public void handleCheckBoxClick(View view) {
    CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
    if(tmpChkBox.isChecked())
    {
        tmpChkBox.setBackgroundColor(color.blue);
    }
    else
    {
        tmpChkBox.setBackgroundColor(color.red);
    }

    System.out.println(view.getId() + " " + tmpChkBox.isChecked());

    switch (view.getId()) {
    case R.id.chkLargeIcons:
        ...     
    break;
        ...
    }
    ...
}

我应该在code改变什么,使其工作,因为我想要的吗?

What should i change in my code to make it work as i want?

在此先感谢您的帮助和建议。

Thanks in advance for any help and suggestions.

推荐答案

一个不清楚的方面首先解释一下

First some explanation of one unclear aspect

事情是,如果有人认为我使用Android的颜色类在我的code提到,例如颜色可能会产生误导。当我使用我的版本的颜色的红色蓝色存储在 color.xml 并这就是为什么我说他们在code作为< STRONG>的 color.red 代替 Col​​or.RED

Thing is that example colors mentioned in my code might be misleading if someone assume that i use android Color class. While i was using my versions of colors red and blue stored in color.xml and thats why I stated them in code as color.red instead of Color.RED.

所以,我的code作为同其他回答听众code,两者如果你认为颜色从Android电子类采取有效。

So my code as same as listener code in other answer, both are valid if you assume that color is taken from android class.

我的问题源为一些奇怪的故障,或不我知Android的行为,导致以下code工作故障。

Source of my problem is some strange glitch, or not known by me android behaviour that causes following code work faulty.

public void handleCheckBoxClick(View view) {
    CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
    if(tmpChkBox.isChecked())
    {
        tmpChkBox.setBackgroundColor(color.blue);
    }
    else
    {
        tmpChkBox.setBackgroundColor(color.red);
    }
}

precisely线像这样的

    tmpChkBox.setBackgroundColor([ColorFromResources]);

虽然code的执行给我颜色的变化只是一个occurence,那么它只是呆在那样,完全地反应迟钝。那一个变化,主要是误导性的,让我停留在这个问题上很长一段时间,甚至让我把它留给后来固定。
一些调查研究后,我想通了按照code修改解决我的问题:

   tmpChkBox.setBackgroundColor(getResources().getColor([ColorFromResources]));

现在所有的作品就像一个魅力。但我仍然不解,为什么从资源色通话效果,而不 getResources()。的getColor(...)。希望这回答将帮助别人是很奇怪的卡住我。

Now all works like a charm. But I'm still puzzled why does call of color from resources works without getResources().getColor(...). Hopefully that answer will help someone as strangely stuck as me.

这篇关于无法更改复选框鉴于背景颜色两次 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆