用Java在Android Studio中对复选框的值求和?

huangapple go评论124阅读模式
英文:

Sum value with checkboxes in android studio with java?

问题

在我的应用程序中,我使用复选框来计算总和值。如果 "checkbox1.isChecked",我会将值添加到所有数据值的总和中。当 (!checkBox1.isChecked) 时,我不想添加。

1.) 如果我选中 checkBox1 和 checkBox2,我的总和 = 300,但如果我取消选中 checkbox1,它会显示为 0.0。(屏幕截图 1 和 2)

2.) 如果我首先选中了 checkBox1,然后再选中 checkedBox2,它会显示 checkBox2 的值。(屏幕截图 3 和 4)

这是我的代码?我应该做什么更改才能使其正常工作(选中时添加,未选中时不添加)

float averageOfMondays = 200;
float averageOfTuesdays = 100;
float daysSum = 0;

checkBox1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkBox1.isChecked()) {
            monday = true;
            daysSum += averageOfMondays;
            averageCounter++;
        } else {
            monday = false;

            if (averageCounter <= 0) {
                averageCounter = 0;
            } else {
                averageCounter--;
            }
        }
    }
});

checkBox2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkBox2.isChecked()) {
            tuesday = true;
            daysSum += averageOfTuesdays;
            averageCounter++;
        } else {
            tuesday = false;
            if (averageCounter <= 0) {
                averageCounter = 0;
            } else {
                averageCounter--;
            }
        }
    }
});

btnCheck.setOnClickListener(new View.OnClickListener() { //button CHECK 
    @Override
    public void onClick(View v) {

        if (averageCounter == 0) {
            tvAverage.setText("Please choose at least 1 day");
        } else {
            tvAverage.setText(String.valueOf(daysSum));
            daysSum = 0;
        }
    }
});
英文:

In my application i use chceck boxes to calculate sum value. If "checkbox1.isChecked" i add value to sum of all data values. When (!checkBox1.isChecked) i dont want to add.
1.)If i do checkBox1 and chceckBox2 my Sum = 300,but if i uncheck checkbox1 it shows me 0.0.(screenshot 1 and 2 )

用Java在Android Studio中对复选框的值求和?用Java在Android Studio中对复选框的值求和?

2.)if i first had checked checkBox1 than i add checkedBox2 it shows me value of checkBox2.(screenshot 3 and 4 )

用Java在Android Studio中对复选框的值求和?
用Java在Android Studio中对复选框的值求和?

Here is my code? What should a change to make it work properly (add if checked, not add if not checked)


float averageOfMondays = 200;
float averageOfTuesdays = 100;
float daysSum = 0;
checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox1.isChecked()){
monday = true;
daysSum+=averageOfMondays;
averageCounter++;
}else  {
monday = false;
if(averageCounter&lt;=0){ averageCounter = 0; }else{averageCounter--;}
}
}
});
checkBox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox2.isChecked()){
tuesday = true;
daysSum+=averageOfTuesdays;
averageCounter++;
}else {
tuesday = false;
if(averageCounter&lt;=0){ averageCounter = 0; }else{averageCounter--;}
}
}
});
btnCheck.setOnClickListener(new View.OnClickListener() {            //button CHECK 
@Override
public void onClick(View v) {
if(averageCounter == 0){
tvAverage.setText(&quot;Please choose at least 1 day&quot;);
} else{
tvAverage.setText(String.valueOf(daysSum));
daysSum=0;
}
}
});

答案1

得分: 1

float averageOfMondays = 200;
float averageOfTuesdays = 100;
float daysSum = 0;

checkBox1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkBox1.isChecked()) {
            monday = true;
            daysSum += averageOfMondays;
            averageCounter++;
        } else {
            monday = false;
            averageCounter--;
            daySum -= averageOfMondays;
        }
    }
});

checkBox2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkBox2.isChecked()) {
            tuesday = true;
            daysSum += averageOfTuesdays;
            averageCounter++;
        } else {
            tuesday = false;
            averageCounter--;
            daySum -= averageOfTuesdays;
        }
    }
});

btnCheck.setOnClickListener(new View.OnClickListener() { //button CHECK 
    @Override
    public void onClick(View v) {
        if (averageCounter == 0) {
            tvAverage.setText("Please choose at least 1 day");
        } else {
            tvAverage.setText(String.valueOf(daysSum));
        }
    }
});

This way you can have the correct functionality.
英文:
float averageOfMondays = 200;
float averageOfTuesdays = 100;
float daysSum = 0;
checkBox1.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
if (checkBox1.isChecked()) {
monday = true;
daysSum += averageOfMondays;
averageCounter++;
} else {
monday = false;
averageCounter--;
daySum -= averageOfMondays;
}
}
});
checkBox2.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
if (checkBox2.isChecked()) {
tuesday = true;
daysSum += averageOfTuesdays;
averageCounter++;
} else {
tuesday = false;
averageCounter--;
daySum -= averageOfTuesdays;
}
}
});
btnCheck.setOnClickListener(new View.OnClickListener() { //button CHECK 
@Override
public void onClick(View v) {
if (averageCounter == 0) {
tvAverage.setText(&quot;Please choose at least 1 day&quot;);
} else {
tvAverage.setText(String.valueOf(daysSum));
}
}
});

This way you can have the correct functionality.

huangapple
  • 本文由 发表于 2020年10月20日 18:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64442798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定