Android权威指南第二版课后练习2.10

 
  
 
  

QuizActivity.java

package com.example.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private Button mBackButton;
    private TextView mQuestionTextViwe;

    //将多个问题存入数组以及答案通过new对象后存入数组
    private Question[] mQuestionBank = new Question[]{
            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_amaricas, true),
            new Question(R.string.question_asia, true),
    };
    //更新的问题的updateQuestions()方法
    private void updateQuestions(){
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextViwe.setText(question);
    }

    //答案判定方法
    private void checkAnswer(boolean userPressTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId = 0;
        if(userPressTrue == answerIsTrue){
            messageResId = R.string.correct_toast;
        }else {
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(QuizActivity.this, messageResId , Toast.LENGTH_SHORT).show();
    }

    private int mCurrentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        mQuestionTextViwe = (TextView) findViewById(R.id.question_text_view);
        mQuestionTextViwe.setClickable(true);
        mQuestionTextViwe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestions();
            }
        });
        mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });
        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });
        mNextButton = (Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestions();
            }
        });

        mBackButton = (Button) findViewById(R.id.back_button);
        mBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果一开始就按Back会导致数组越界异常,所以进行一个处理
                //当mCurrentIndex为0,将数组长度减一赋值给mCurrentIndex,可以避免数组越界异常
                if(mCurrentIndex == 0){
                    mCurrentIndex = mQuestionBank.length - 1;
                    updateQuestions();
                }else {
                    mCurrentIndex = (mCurrentIndex -1 ) % mQuestionBank.length;
                    updateQuestions();
                }
            }
        });
        //会显示第一个问题
        updateQuestions();

    }
}


你可能感兴趣的:(Android权威指南第二版课后练习2.10)