我有一个“值”页面,其中存储了每个单选按钮和复选框的得分。在测验的末尾,我有一个重置按钮,测验会重置,但是分数没有。单击按钮后,如何将分数重置为零?任何帮助将不胜感激。
价值观活动
public class Values {
private static Values instance;
private int QUESTIONS_COUNT = 39;
private int[] scores = new int[QUESTIONS_COUNT];
private Values(){
}
public static Values getInstance(){
if(instance == null){
instance = new Values();
}
return instance;
}
public int updateScore(int checkedId) {
switch(checkedId){
case R.id.checkBoxQ15MSh:
scores[34] = 5;
return scores[34];
case R.id.checkBoxQ15MSi:
scores[35] = 5;
return scores[35];
case R.id.checkBoxQ15MSj:
scores[36] = 5;
return scores[36];
case R.id.checkBoxQ15MSk:
scores[37] = 5;
return scores[37];
case R.id.checkBoxQ15MSl:
scores[38] = 5;
return scores[38];
case R.id.checkBoxQ15MSm:
scores[39] = 5;
return scores[39];
}
return 0;
}
public int totalScore() {
int totalScore = 0;
for (int score : scores) {
totalScore += score;
}
return totalScore;
}
}
这是带有测验和重置按钮的片段
CBS23.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int totalScore = Values.getInstance().updateScore(CBS23.getId());
if (CBS23.isChecked()) {
score = score + totalScore;
} else if (!CBS23.isChecked()) {
score = score - totalScore;
tvS.setText(" " + score);
}
}
});
CBS24.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int totalScore = Values.getInstance().updateScore(CBS24.getId());
if (CBS24.isChecked()) {
score = score + totalScore;
} else if (!CBS24.isChecked()) {
score = score - totalScore;
tvS.setText(" " + score);
}
}
});
CBS25.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int totalScore = Values.getInstance().updateScore(CBS25.getId());
if (CBS25.isChecked()) {
score = score + totalScore;
} else if (!CBS25.isChecked()) {
score = score - totalScore;
tvS.setText(" " + score);
}
}
});
CBS26.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int totalScore = Values.getInstance().updateScore(CBS26.getId());
if (CBS26.isChecked()) {
score = score + totalScore;
} else if (!CBS26.isChecked()) {
score = score - totalScore;
tvS.setText(" " + score);
}
}
});
CBS27.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int totalScore = Values.getInstance().updateScore(CBS27.getId());
if (CBS27.isChecked()) {
score = score + totalScore;
} else if (!CBS27.isChecked()) {
score = score -totalScore;
tvS.setText(" " + score);
}
}
});
CBS28.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int totalScore = Values.getInstance().updateScore(CBS28.getId());
if (CBS28.isChecked()) {
score = score + totalScore;
} else if (!CBS28.isChecked()) {
score = score - totalScore;
tvS.setText(" " + score);
}
}
});
btnrestartS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),MainActivity.class);
startActivity(intent);
getActivity().finish();
}
});
答案 0 :(得分:0)
只需将score
的{{1}}下的onclick
重置为0
类似:
btnrestartS.setOnClickListener(new View.OnClickListener()