Android答题APP按钮颜色异常:正确选项点击后变红而非绿色
问题描述
我正在为期末项目开发一款答题应用,除按钮功能外其余功能均运行正常。按照代码逻辑,点击正确答案的按钮应变为绿色,错误选项按钮应变为红色,但实际操作中,点击正确选项时按钮却变为红色。
package com.example.final_gameplaydb; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.animation.Animator; import android.annotation.SuppressLint; import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.animation.DecelerateInterpolator; import android.widget.TextView; import android.widget.Button; import android.widget.Toast; import com.example.final_gameplaydb.R.id; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.QueryDocumentSnapshot; import com.google.firebase.firestore.QuerySnapshot; import java.util.ArrayList; import java.util.List; public class GameplayActivity extends AppCompatActivity implements View.OnClickListener { TextView question, timerNum, questionCount; Button first, second, third, fourth; List<Question> questionList; int questionNum, scoreNum; CountDownTimer countDown; FirebaseFirestore firestore = FirebaseFirestore.getInstance(); boolean quizEnded = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gameplay); question = findViewById(R.id.Question); questionCount = findViewById(R.id.QuestionNum); timerNum = findViewById(R.id.TimerNum); first = findViewById(R.id.FirstChoice); second = findViewById(R.id.SecondChoice); third = findViewById(R.id.ThirdChoice); fourth = findViewById(R.id.FourthChoice); first.setOnClickListener(this); second.setOnClickListener(this); third.setOnClickListener(this); fourth.setOnClickListener(this); getQuestionsList(); scoreNum = 0; countDown = new CountDownTimer(60000, 1000) { @Override public void onTick(long millisUntilFinished) { timerNum.setText(String.valueOf(millisUntilFinished / 1000)); if (millisUntilFinished <= 0) { countDown.cancel(); } } @Override public void onFinish() { if(!quizEnded) { quizEnded = true; Intent intent = new Intent(GameplayActivity.this, ScoreActivity.class); startActivity(intent); finish(); } } }.start(); } private void getQuestionsList() { questionList = new ArrayList<>(); firestore.collection("Difficulties").document("Categories") .collection("Easy") .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { QuerySnapshot questions = task.getResult(); for(QueryDocumentSnapshot doc : questions){ questionList.add(new Question(doc.getString("Question"), doc.getString("A"), doc.getString("B"), doc.getString("C"), doc.getString("D"), doc.getString("Answer"))); } setQuestion(); } else { Log.e("FirestoreError", "Error getting documents: ", task.getException()); Toast.makeText(GameplayActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show(); } } }); } private void setQuestion() { //timerNum.setText(String.valueOf(60)); question.setText(questionList.get(0).getQuestion()); first.setText(questionList.get(0).getOptionA()); second.setText(questionList.get(0).getOptionB()); third.setText(questionList.get(0).getOptionC()); fourth.setText(questionList.get(0).getOptionD()); questionCount.setText(String.valueOf(1)); questionNum = 0; } @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { String selectChoice = ""; if (v.getId() == id.FirstChoice) { selectChoice = "first"; } else if (v.getId() == id.SecondChoice) { selectChoice = "second"; } else if (v.getId() == id.ThirdChoice) { selectChoice = "third"; } else if (v.getId() == id.FourthChoice) { selectChoice = "fourth"; } checkAnswer(selectChoice, v); } private void checkAnswer(String selectChoice, View view) { Button selectedButton = (Button) view; if (selectChoice.equals(questionList.get(questionNum).getCorrectAns())) { // right answer selectedButton.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN)); scoreNum++; } else { // wrong answer selectedButton.setBackgroundTintList(ColorStateList.valueOf(Color.RED)); // Set the background tint of the correct answer to green switch (questionList.get(questionNum).getCorrectAns()) { case "first": first.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN)); break; case "second": second.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN)); break; case "third": third.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN)); break; case "fourth": fourth.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN)); break; } } // Delay for 1000 milliseconds before changing the question new Handler().postDelayed(new Runnable() { @Override public void run() { selectedButton.setEnabled(true); // Reset background tint of all buttons before changing the question selectedButton.setBackgroundTintList(null); first.setBackgroundTintList(null); second.setBackgroundTintList(null); third.setBackgroundTintList(null); fourth.setBackgroundTintList(null); changeQuestion(); } }, 0500); } private void changeQuestion() { if (questionNum < questionList.size() - 1) { questionNum++; playAnim(question, 0, 0); playAnim(first, 0, 1); playAnim(second, 0, 2); playAnim(third, 0, 3); playAnim(fourth, 0, 4); questionCount.setText(String.valueOf(questionNum + 1)); //timerNum.setText(String.valueOf(60)); } else { // go to score activity Intent intent = new Intent(GameplayActivity.this, ScoreActivity.class); intent.putExtra("SCORE", String.valueOf(scoreNum)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); GameplayActivity.this.finish(); } } private void playAnim(final View view, final int value, final int viewNum) { view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500) .setStartDelay(100).setInterpolator(new DecelerateInterpolator()) .setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { if (value == 0){ switch (viewNum){ case 0: ((TextView)view).setText(questionList.get(questionNum).getQuestion()); break; case 1: ((Button)view).setText(questionList.get(questionNum).getOptionA()); break; case 2: ((Button)view).setText(questionList.get(questionNum).getOptionB()); break; case 3: ((Button)view).setText(questionList.get(questionNum).getOptionC()); break; case 4: ((Button)view).setText(questionList.get(questionNum).getOptionD()); break; } /**if (viewNum == 0){ ((Button)view).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#03A9F4"))); }**/ playAnim(view,1,viewNum); } } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); } }
问题分析与修复
核心问题在于Firestore存储的Answer字段值和代码中判断的字符串不匹配:
你将用户点击的按钮映射为"first"/"second"/"third"/"fourth",但如果Firestore中Answer字段存的是"A"/"B"/"C"/"D"或者选项的具体文本,就会导致正确选项被判定为错误,从而触发红色背景逻辑。
具体修复方案
1. 匹配Firestore数据格式
根据你的Firestore实际存储情况修改映射逻辑:
- 如果
Answer字段存的是"A"/"B"/"C"/"D":@SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { String selectChoice = ""; if (v.getId() == id.FirstChoice) { selectChoice = "A"; } else if (v.getId() == id.SecondChoice) { selectChoice = "B"; } else if (v.getId() == id.ThirdChoice) { selectChoice = "C"; } else if (v.getId() == id.FourthChoice) { selectChoice = "D"; } checkAnswer(selectChoice, v); } - 如果
Answer字段存的是选项的完整文本:@SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { Button selectedBtn = (Button) v; String selectChoice = selectedBtn.getText().toString(); checkAnswer(selectChoice, v); }
2. 修复延迟时间语法错误
代码中0500是八进制数(对应十进制320),如果要设置500毫秒延迟,直接写500:
new Handler().postDelayed(new Runnable() { @Override public void run() { // 原有逻辑 } }, 500);
3. 添加调试日志(可选)
在checkAnswer方法开头添加日志,方便验证判断逻辑的输入值:
private void checkAnswer(String selectChoice, View view) { Log.d("AnswerCheck", "用户选择: " + selectChoice + " | 正确答案: " + questionList.get(questionNum).getCorrectAns()); // 原有逻辑 }
内容的提问来源于stack exchange,提问作者yyweixh
相关产品推荐
相关产品推荐

