在Android的复选框,设置背景颜色 [英] Setting background colour on check box in android

查看:793
本文介绍了在Android的复选框,设置背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置的时候就已经选中,则复选框的背景颜色,如果它未被选中,然后删除该颜色。

I'm trying to set the background colour of a check box when it has been checked then and if it is unchecked then to remove that colour.

有人能帮我找到复选框的ID,然后在检查它是否被选中。我在XML表 Android的设置:的onClick =handleCheckBoxClick

Can anybody help me find the Id of the checkbox then to check if it is checked. I have set in my XML sheet android:onClick="handleCheckBoxClick"

有关你得到它的外观我有大约25复选框的想法。
这是我需要同样做给他们时,他们是pressed。

For you to get an idea of how it looks I have roughly 25 check boxes. which I need the same doing to them when they are pressed.

时有可能有code的一个块或将需要重复每个复选框?

Is it possible to have one block of code or would it need to be repeated for each checkbox?

编辑:
忘了说,这是一个片段中(如果有什么差别)

Forgot to mention this is within a fragment (if that makes any difference)

public void handleCheckBoxClick(View view) {
        int chkID = view.getId();

        if (){

        }else{

        }

        //find which checkbox was checked then get its id
        /*
         * if checked then
         * change background colour to blue
         * if unchecked then
         * remove background colour
         * 
         */
    }

彩色部分我会尝试自己当需要学习。

The colour part i'll try myself as do need to learn .

推荐答案

使用以下code。

public void handleCheckBoxClick(View view) {
    CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
    if(tmpChkBox.isChecked())
    {
        tmpChkBox.setBackgroundColor(Color.BLUE);
    }
    else
    {
        tmpChkBox.setBackgroundColor(Color.RED);
    }
}

这篇关于在Android的复选框,设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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