取决于复选框的计数器

时间:2018-09-06 00:01:41

标签: java android

如果复选框为check,我想增加整数,如果不是,则减少。 我的尝试:

def fun1(obj, attrib):
    setattr(obj, attrib, 1)

我不知道,也许整数没有增加-减少,或者我无法访问整数。我该怎么办?

3 个答案:

答案 0 :(得分:0)

您应该在if (a == 1)内调用支票(onCheckedChanged() ...),否则,它永远不会被调用。

答案 1 :(得分:0)

触发复选框后,您需要调用if(a == 1)..if(a == 2)

因此创建一个方法checkValueMethod(int checkValue)

private void checkValueMethod(int a) {
        if (a == 1){
            CompoundButtonCompat.setButtonTintList(c, ColorStateList.valueOf(this.getResources().getColor(R.color.black)));
            CompoundButtonCompat.setButtonTintList(c1, ColorStateList.valueOf(this.getResources().getColor(R.color.black)));
        }
        if (a == 2) {
            CompoundButtonCompat.setButtonTintList(c, ColorStateList.valueOf(this.getResources().getColor(R.color.reject_red)));
            CompoundButtonCompat.setButtonTintList(c1, ColorStateList.valueOf(this.getResources().getColor(R.color.reject_red)));
        }
    }

并在您选中复选框时调用此方法

 @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                a++;
            } else {
                a--;

            }
             checkValueMethod(a);
        }
    });

    c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                a++;
            } else {
                a--;

            }
            checkValueMethod(a);
        }
    });

答案 2 :(得分:0)

让我们使用4个复选框更加清楚。首先,我们的复选框名称为

checkbox1

checkbox2,

checkbox3和

复选框4。

我们希望如果选中了四个复选框,则复选框的选中颜色将设置为红色。

如果有3 =橙色,

如果有2 =黄色

如果有1 =绿色,

我们必须在colors.xml中定义颜色

我这样定义了这些颜色:

<color name="blue">#0DFFDC</color>
<color name="green">#B8FF0D</color>
<color name="yellow">#FFF90D</color>
<color name="orange">#FFA00D</color>
<color name="red">#FF3A0D</color>

我们必须使用此代码来更改复选框的颜色:

CompoundButtonCompat.setButtonTintList(MYCHECKBOXNAME, ColorStateList.valueOf(context.getResources().getColor(R.color.MYCOLOR)));

并且我们必须定义多个checkedChangeListener。就是这样:

    CompoundButton.OnCheckedChangeListener multiListener = new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton v, boolean isChecked) {
    switch (v.getId()){
    case R.id.checkbox1:
    //stuff
    break;
    case R.id.checkbox2:
    //stuff
    break;
    case R.id.checkbox3:
    //stuff
    break;
    case R.id.checkbox4:
    //stuff
    break;
                            }
                        }
                    };

我们有一个整数(它是a),如果要选中此复选框,我们想增加它的值,如果不选中此复选框,我们要减小它的值。 我们可以在每种情况下使用以下代码:

if (isChecked) {
a++;
} else {
a--;
}

a正确地增加和减少了。

让我们使用变色代码: 对于红色:

if (a == 4){
CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
}

对于橙色:

if (a == 3){
    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
    }

黄色:

if (a == 2){
    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
    }

对于绿色:

if (a == 1){
    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
    }

最终状态是:

a = 0;
        CompoundButton.OnCheckedChangeListener multiListener = new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton v, boolean isChecked) {
                switch (v.getId()){
                    case R.id.checkbox1:
                        if (isChecked) {
                        a++;
                    }
                    else {
                        a--;
                    }
                        break;
                    case R.id.checkbox2:
                        if (isChecked) {
                            a++;
                        }
                        else {
                            a--;
                        }
                        break;
                    case R.id.checkbox3:
                        if (isChecked) {
                            a++;
                        }
                        else {
                            a--;
                        }
                        break;
                    case R.id.checkbox4:
                        if (isChecked) {
                            a++;
                        }
                        else {
                            a--;
                        }
                        break;
                }
                if (a == 4){
                    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
                    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
                    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
                    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
                }
                if (a == 3){
                    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
                    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
                    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
                    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
                }
                if (a == 2){
                    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
                    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
                    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
                    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.yellow)));
                }
                if (a == 1){
                    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
                    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
                    CompoundButtonCompat.setButtonTintList(checkbox3, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
                    CompoundButtonCompat.setButtonTintList(checkbox4, ColorStateList.valueOf(context.getResources().getColor(R.color.green)));
                }
            }
        };

 checkbox1.setOnCheckedChangeListener(multiListener);
                checkbox2.setOnCheckedChangeListener(multiListener);
                checkbox3.setOnCheckedChangeListener(multiListener);
                checkbox4.setOnCheckedChangeListener(multiListener);

申请投票!