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

Android Studio(Java)答题APP:对错选项颜色提示及跳题功能实现

Android答题APP选项交互功能优化方案

需求说明

  • 选择正确选项:该选项背景变为绿色,延迟后切换至下一题
  • 选择错误选项:选中的错误选项背景变为红色,正确选项背景变为绿色,延迟后切换至下一题

原代码存在的问题

  1. 字符串比较错误:使用==比较字符串内容,实际应使用equals()(==仅比较对象引用,不匹配字符串内容)
  2. 重复代码冗余:四个选项的点击事件逻辑高度重复,维护成本高
  3. 无反馈延迟:切换题目过于迅速,用户无法看到颜色反馈
  4. 状态未重置:切换题目时未清除上一题的选项背景色,导致状态残留
  5. 错误选项无红色标记:未实现错误选项变红的需求

修改后的完整代码实现

1. 提取通用选项点击处理方法

private void handleOptionClick(View selectedOption, String selectedText) {
    // 禁用所有选项,防止反馈期间重复点击
    option1.setEnabled(false);
    option2.setEnabled(false);
    option3.setEnabled(false);
    option4.setEnabled(false);

    if (selectedText.equals(mAnswer)) {
        // 正确选项逻辑
        selectedOption.setBackgroundColor(Color.GREEN);
        mScore++;
        scores.setText("Score: " + mScore);
    } else {
        // 错误选项逻辑:标记错误选项为红色,正确选项为绿色
        selectedOption.setBackgroundColor(Color.RED);
        highlightCorrectOption();
    }

    // 延迟1秒切换下一题,确保用户看到反馈效果
    new Handler(Looper.getMainLooper()).postDelayed(() -> {
        resetOptionStates();
        updateQuestions(r.nextInt(mQuestionsLength));
    }, 1000);
}

// 标记正确选项为绿色
private void highlightCorrectOption() {
    if (option1.getText().equals(mAnswer)) {
        option1.setBackgroundColor(Color.GREEN);
    } else if (option2.getText().equals(mAnswer)) {
        option2.setBackgroundColor(Color.GREEN);
    } else if (option3.getText().equals(mAnswer)) {
        option3.setBackgroundColor(Color.GREEN);
    } else if (option4.getText().equals(mAnswer)) {
        option4.setBackgroundColor(Color.GREEN);
    }
}

// 重置选项的背景色与可点击状态
private void resetOptionStates() {
    // 替换为你APP中选项的默认背景色,比如Color.WHITE或主题默认色
    option1.setBackgroundColor(Color.WHITE);
    option2.setBackgroundColor(Color.WHITE);
    option3.setBackgroundColor(Color.WHITE);
    option4.setBackgroundColor(Color.WHITE);
    
    option1.setEnabled(true);
    option2.setEnabled(true);
    option3.setEnabled(true);
    option4.setEnabled(true);
}

2. 简化选项点击事件绑定

// 替换原有的四个重复点击事件
option1.setOnClickListener(v -> handleOptionClick(option1, option1.getText().toString()));
option2.setOnClickListener(v -> handleOptionClick(option2, option2.getText().toString()));
option3.setOnClickListener(v -> handleOptionClick(option3, option3.getText().toString()));
option4.setOnClickListener(v -> handleOptionClick(option4, option4.getText().toString()));

3. 更新updateQuestions方法,确保状态重置

private void updateQuestions(int num) {
    resetOptionStates(); // 每次切换题目前重置选项状态
    question.setImageResource(mQuestions.getQuestion(num));
    option1.setText(mQuestions.getChoice1(num));
    option2.setText(mQuestions.getChoice2(num));
    option3.setText(mQuestions.getChoice3(num));
    option4.setText(mQuestions.getChoice4(num));

    mAnswer = mQuestions.getCorrectAnswer(num);
}

4. 保留原有的gameOver方法(无需修改)

private void gameOver(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilder
            .setMessage("Game Over! Your Score is " + mScore + " points.")
            .setCancelable(false)
            .setPositiveButton("NEW GAME",
                    (dialogInterface, i) -> {
                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                        finish();
                    })
            .setNegativeButton("EXIT",
                    (dialogInterface, i) -> finish());
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

关键优化点说明

  • 字符串比较修复:使用equals()确保字符串内容匹配,避免判断错误
  • 代码复用:通过通用方法减少重复代码,后续修改逻辑只需维护一处
  • 用户体验优化:加入1秒延迟,让用户清晰看到选项颜色反馈
  • 状态管理:每次切换题目或处理完点击后重置选项状态,避免界面残留问题
  • 交互限制:反馈期间禁用所有选项,防止重复操作导致逻辑混乱

内容的提问来源于stack exchange,提问作者Kashaf Ali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:36:21