在Android的复选框,设置背景颜色复选框、颜色、背景、Android

2023-09-06 09:36:57 作者:待我长发及屁股

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

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);
    }
}