我正在开发一个QuizApp。它包含多个选项问题,单选按钮作为选项。我的问题是例如我在问题1当我点击正确的答案点击下一个按钮(问题2)并再次点击后退按钮然后转到问题1然后点击正确的答案我的分数正在增加两倍..任何人都可以帮助我我是Android的新手..提前谢谢。下面是我的代码。
public class TrueFalseActivity extends AppCompatActivity {
List<QuestionTrueFalse> questionTrueFalseList;
int myscore = 0;
int quid = 0;
int id;
TextView tv, txt1;
QuestionTrueFalse curQues,c;
RadioButton b1, b2;
Button next;
RadioGroup grp;
RadioButton answer;
String string;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trfal);
tv = (TextView) findViewById(R.id.tv1);
txt1 = (TextView) findViewById(R.id.txt);
b1 = (RadioButton) findViewById(R.id.radio1);
b2 = (RadioButton) findViewById(R.id.radio2);
submit=(Button) findViewById(R.id.sub);
DbHelper db = new DbHelper(this);
grp = (RadioGroup) findViewById(R.id.radiogroup1);
questionTrueFalseList = db.getAllTrFalsQuestions();
if (questionTrueFalseList != null && questionTrueFalseList.size() != 0) {
curQues = questionTrueFalseList.get(quid);
setQuestionView();
}
next = (Button) findViewById(R.id.front);
back = (Button) findViewById(R.id.back);
//Next Button
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
try {
if (curQues.getAnsw().equals(answer.getText())) {
myscore++;
Log.d("score", "Your score" + myscore);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Select any option", Toast.LENGTH_LONG).show();
}
c = curQues;
if (quid < 19) {
quid++;
curQues = questionTrueFalseList.get(quid);
grp.clearCheck();
setQuestionView();
}
else {
countDownTimer.cancel();
timerHasStarted = false;
Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", myscore); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
//Submit Button
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
grp = (RadioGroup) findViewById(R.id.radiogroup1);
answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
// save();
try {
if (curQues.getAnsw().equals(answer.getText())) {
myscore++;
Log.d("score", "Your score" + myscore);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Select any option", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(TrueFalseActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", myscore); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
});
//Back button
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quid > 0) {
quid--;
curQues = questionTrueFalseList.get(quid);
setQuestionView();
}
}
});
}
private void setQuestionView() {
tv.setText(curQues.getQues());
b1.setText(curQues.getOpt1());
b2.setText(curQues.getOpt2());
}
}
//我的问题是,当我选择一个正确的答案选项时转到下一个问题并回到相同的问题并再次选择正确的答案我的分数正在增加两次.am使用单选按钮。如何解决这个问题请帮助我。
答案 0 :(得分:0)
创建一个新的布尔变量
boolean isIncremented=false;
//in you submit button change this logic
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//rest of the code
try {
if (curQues.getAnsw().equals(answer.getText())) {
if(!isIncremented){
myscore++;
isIncremented=!isIncremented;
}
//rest of the code
}
//inside your next button do the same
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//rest if the code
try {
if (curQues.getAnsw().equals(answer.getText())) {
if(!isIncremented){
myscore++;
isIncremented=!isIncremented;
}
//rest if the code
}
//and don't forget to change the value of isIncremented set to be false again when new question is generated