英文:
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 )
2.)if i first had checked checkBox1 than i add checkedBox2 it shows me value of checkBox2.(screenshot 3 and 4 )
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<=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;
}
}
});
答案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("Please choose at least 1 day");
} else {
tvAverage.setText(String.valueOf(daysSum));
}
}
});
This way you can have the correct functionality.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论