You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android测验应用功能优化求助:防重复作答+百分比最终得分

嘿,这两个功能在测验类App里太实用了!我给你梳理下具体的实现思路和代码示例,直接就能套进你的项目里~

实现作答后禁用题目+百分比得分方案

一、作答后禁用题目,防止重复提交

核心思路是记录每道题的作答状态,并在用户作答后禁用对应控件,同时阻止重复触发逻辑。

具体步骤:

  1. 声明一个布尔数组来跟踪每道题是否已作答:
    private boolean[] answeredQuestions;
    
  2. 在onCreate方法里初始化数组,长度和你的总题数一致:
    answeredQuestions = new boolean[TOTAL_QUESTIONS]; // TOTAL_QUESTIONS是你的总题数常量
    
  3. 给每道题的交互控件(比如按钮、单选组)设置点击/选择事件,在事件处理中先判断是否已作答:
    private void handleQuestionAnswer(int questionIndex, boolean isCorrect) {
        // 已作答直接返回,不处理
        if (answeredQuestions[questionIndex]) return;
        
        // 标记为已作答
        answeredQuestions[questionIndex] = true;
        
        // 禁用当前题目控件(以Button为例,其他控件类似调整)
        Button targetBtn = findViewById(getQuestionId(questionIndex));
        targetBtn.setEnabled(false);
        targetBtn.setAlpha(0.5f); // 变灰增强视觉提示
        
        // 这里可以添加你的答案判断逻辑,比如统计答对数量
        if (isCorrect) correctAnswers++;
    }
    

二、计算并显示百分比形式的最终得分

核心是统计答对题数,然后用「答对数/总题数*100」得到百分比,注意避免整数除法的坑。

具体实现:

  1. 先声明变量统计答对数量:
    private int correctAnswers = 0;
    
  2. 在每道题作答正确时递增该变量(如上一步的代码)。
  3. 当所有题目都答完后,计算并显示得分:
    private void showFinalScore() {
        // 用100.0f确保浮点运算,避免整数除法导致结果为0
        float scorePercent = (correctAnswers * 100.0f) / TOTAL_QUESTIONS;
        // 显示到TextView,保留1位小数更友好
        TextView scoreTv = findViewById(R.id.tv_final_score);
        scoreTv.setText(String.format("最终得分:%.1f%%", scorePercent));
    }
    
  4. 可以加一个判断所有题目是否完成的方法:
    private boolean isAllQuestionsAnswered() {
        for (boolean answered : answeredQuestions) {
            if (!answered) return false;
        }
        return true;
    }
    
    然后在每道题作答后调用这个方法,返回true时就触发showFinalScore()。

完整示例代码(适配你的项目结构)

package com.example.randomuser.androidquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class QuizActivity extends AppCompatActivity {
    private static final int TOTAL_QUESTIONS = 5; // 替换成你的实际题数
    private boolean[] answeredQuestions;
    private int correctAnswers = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        
        // 初始化作答状态数组
        answeredQuestions = new boolean[TOTAL_QUESTIONS];
        
        // 绑定题目控件和事件(示例,根据你的布局调整ID)
        bindQuestionEvent(R.id.btn_q1, 0, true); // 第1题,正确答案为true
        bindQuestionEvent(R.id.btn_q2, 1, false); // 第2题,正确答案为false
        // 继续绑定其他题目...
    }

    private void bindQuestionEvent(int btnId, int questionIndex, boolean isCorrect) {
        Button btn = findViewById(btnId);
        btn.setOnClickListener(v -> {
            handleQuestionAnswer(questionIndex, isCorrect);
        });
    }

    private void handleQuestionAnswer(int questionIndex, boolean isCorrect) {
        if (answeredQuestions[questionIndex]) return;
        
        answeredQuestions[questionIndex] = true;
        Button targetBtn = findViewById(getQuestionBtnId(questionIndex));
        targetBtn.setEnabled(false);
        targetBtn.setAlpha(0.5f);
        
        if (isCorrect) correctAnswers++;
        
        // 所有题答完就显示得分
        if (isAllQuestionsAnswered()) {
            showFinalScore();
        }
    }

    private int getQuestionBtnId(int index) {
        // 根据索引返回对应控件ID,替换成你的实际ID
        switch (index) {
            case 0: return R.id.btn_q1;
            case 1: return R.id.btn_q2;
            case 2: return R.id.btn_q3;
            case 3: return R.id.btn_q4;
            case 4: return R.id.btn_q5;
            default: return -1;
        }
    }

    private boolean isAllQuestionsAnswered() {
        for (boolean answered : answeredQuestions) {
            if (!answered) return false;
        }
        return true;
    }

    private void showFinalScore() {
        float scorePercent = (correctAnswers * 100.0f) / TOTAL_QUESTIONS;
        TextView scoreTv = findViewById(R.id.tv_final_score);
        scoreTv.setText(String.format("最终得分:%.1f%%", scorePercent));
    }
}

额外注意事项

  • 如果你的题目用的是RadioGroup或CheckBox,禁用逻辑改成radioGroup.setEnabled(false)即可,同样可以调整透明度增强提示。
  • 屏幕旋转时记得用onSaveInstanceState保存answeredQuestions和correctAnswers,避免数据丢失:
    @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putBooleanArray("answeredQuestions", answeredQuestions);
          outState.putInt("correctAnswers", correctAnswers);
      }
    
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          answeredQuestions = savedInstanceState.getBooleanArray("answeredQuestions");
          correctAnswers = savedInstanceState.getInt("correctAnswers");
          // 这里还要根据状态重新设置控件的禁用状态,避免旋转后控件恢复可用
          for (int i = 0; i < TOTAL_QUESTIONS; i++) {
              if (answeredQuestions[i]) {
                  Button btn = findViewById(getQuestionBtnId(i));
                  btn.setEnabled(false);
                  btn.setAlpha(0.5f);
              }
          }
      }
    

内容的提问来源于stack exchange,提问作者Mr. B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:05:49