Android编程权威指南挑战——禁止一题多答与评分

思想:在模型层中添加boolean值来记录是否已经答过题,在控制层的重写保存实例方法中put一个和mQuestionBank长度一样大小的boolean[]的数组与记录答题次数以及答题正确的次数,在创建的时候再把他们的值附到mQuestionBank、count、correctCount中,其他逻辑代码如下:

Model层代码:

public class Question {
    private int mTextResId;  //问题文本
    private boolean mAnswerTrue;   //问题答案
    private boolean mIsAnswer; //是否回答过问题

    public Question(int textResId, boolean answerTrue) {
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }

    public int getTextResId() {
        return mTextResId;
    }

    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }

    public boolean isAnswer() {
        return mIsAnswer;
    }

    public void setAnswer(boolean answer) {
        mIsAnswer = answer;
    }
}

Controller层代码:

public class QuizActivity extends AppCompatActivity {
    private static final String TAG = "QuizActivity";
    public static final String KEY_INDEX = "index";
    public static final String REPLY_STATUS = "reply";
    public static final String COUNT = "count";
    public static final String C_COUNT = "correctCount";

    private static int count = 0;
    private static int correctCount = 0;

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;

    private Question[] mQuestionBank = new Question[]{
            new Question(R.string.question_australia, true),
            new Question(R.string.question_oceans, true),
            new Question(R.string.question_mideast, false),
            new Question(R.string.question_africa, false),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true)
    };

    private int mCurrentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: 创建");
        setContentView(R.layout.activity_quiz);

        mQuestionTextView = findViewById(R.id.question_text_view);
        mTrueButton = findViewById(R.id.true_button);
        mFalseButton = findViewById(R.id.false_button);
        mNextButton = findViewById(R.id.next_button);

        if (savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            boolean[] answerList = savedInstanceState.getBooleanArray(REPLY_STATUS);
            for (int i = 0; i < mQuestionBank.length; i++) {
                assert answerList != null;
                mQuestionBank[i].setAnswer(answerList[i]);
            }
            count = savedInstanceState.getInt(COUNT);
            correctCount = savedInstanceState.getInt(C_COUNT);
        }

        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextOne();
            }
        });
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextOne();
            }
        });

        updateQuestion();
    }

    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
        ButtonEnabled();
    }

    private void checkAnswer(boolean userPressedTrue) {
        boolean answerISTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId;

        if (userPressedTrue == answerISTrue) {
            mQuestionBank[mCurrentIndex].setAnswer(true);
            messageResId = R.string.correct_toast;
            correctCount++;
        } else {
            mQuestionBank[mCurrentIndex].setAnswer(true);
            messageResId = R.string.incorrect_toast;
        }
        ButtonEnabled();
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();

        count++;
        if (count == 5) {
            mNextButton.setEnabled(false);
            Toast.makeText(this, "一共答对了" + correctCount + "题,获得" + correctCount * 20 + "分", Toast.LENGTH_LONG).show();
        }
    }

    private void nextOne() {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
        ButtonEnabled();
    }

    private void ButtonEnabled() {
        if (mQuestionBank[mCurrentIndex].isAnswer()) {
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);
        } else {
            mTrueButton.setEnabled(true);
            mFalseButton.setEnabled(true);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState: 保存实例状态");
        outState.putInt(KEY_INDEX, mCurrentIndex);

        boolean[] answerList = new boolean[mQuestionBank.length];
        for (int i = 0; i < answerList.length; i++) {
            answerList[i] = mQuestionBank[i].isAnswer();
        }
        outState.putBooleanArray(REPLY_STATUS, answerList);

        outState.putInt(COUNT,count);
        outState.putInt(C_COUNT,correctCount);
    }

}

 

你可能感兴趣的:(Android)