《Android编程权威指南(第3版)》:挑战练习

 

用户答完某道题,就禁掉那道题对应的按钮,防止用户一题多答。 

用户答完全部题后,显示一个toast消息,给出百分比形式的评分。 

 

Question.java中添加

public class Question {
    private int mTextResId;
    private boolean mAnswerTrue;
    private int mAnswered;

    public Question(int textResId, boolean answerTrue){
        this.mTextResId = textResId;
        this.mAnswerTrue = answerTrue;
        this.mAnswered = 0;
    }

    public int isAnswered(){
        return mAnswered;
    }
    public void setAnswered(int answered){
        mAnswered = answered;
    }
}

QuizActivity中修改部分:

public class QuizActivity extends AppCompatActivity {

    private static final String ANSWERED_LIST = "answered_list";
    private static final String CORRECT_NUM = "correct_num";
    private static final String INCORRECT_NUM = "incorrect_num";
    private int correct = 0;//正确答案数量
    private int incorrect = 0;//错误答案数量
}

 

@Override
    protected void onSaveInstanceState(Bundle saveInstanceState){
            super.onSaveInstanceState(saveInstanceState);
            Log.i(TAG, "onSaveInstanceState");
            saveInstanceState.putInt(KEY_INDEX, mCurrentIndex);
            int[] vis = new int[mQuestionBank.length];
            for (int i = 0; i < mQuestionBank.length; i++)
                vis[i] = mQuestionBank[i].isAnswered();
            saveInstanceState.putIntArray(ANSWERED_LIST, vis);  //保留答题情况
            saveInstanceState.putInt(CORRECT_NUM, correct);     //保留答对题数
            saveInstanceState.putInt(INCORRECT_NUM, incorrect);//保留答错题数
    }
if (savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            int[] vis = savedInstanceState.getIntArray(ANSWERED_LIST);
            for (int i = 0; i < mQuestionBank.length; i++)  //恢复答题情况
                mQuestionBank[i].setAnswered(vis[i]);
            correct = savedInstanceState.getInt(CORRECT_NUM);//恢复答对题数
            incorrect = savedInstanceState.getInt(INCORRECT_NUM);//恢复答错题数
        }

用setEnabled设置按钮状态

public void setButtons(){
        if (mQuestionBank[mCurrentIndex].isAnswered() > 0){
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);
        }else{
            mTrueButton.setEnabled(true);
            mFalseButton.setEnabled(true);
        }
    }
private  void checkfinish(){
        if (correct + incorrect == mQuestionBank.length){
            double ans = (double)correct / (double)mQuestionBank.length * 100.0;
            String mes = "正确率" + String.valueOf(ans) + "%";
            Toast.makeText(this, mes, Toast.LENGTH_SHORT).show();
        }
    }
private void checkAnswer(boolean userPressedTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId = 0;
        mQuestionBank[mCurrentIndex].setAnswered(1);

        if (userPressedTrue == answerIsTrue){
            messageResId = R.string.correct_toast;
            correct++;
        }else{
            messageResId = R.string.incorrect_toast;
            incorrect++;
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    }

 

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

        if (savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            int[] vis = savedInstanceState.getIntArray(ANSWERED_LIST);
            for (int i = 0; i < mQuestionBank.length; i++)  //恢复答题情况
                mQuestionBank[i].setAnswered(vis[i]);
            correct = savedInstanceState.getInt(CORRECT_NUM);//恢复答对题数
            incorrect = savedInstanceState.getInt(INCORRECT_NUM);//恢复答错题数
        }

        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);

        mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
                setButtons();
                checkfinish();
            }
        });

        mFalseButton = (Button)findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                checkAnswer(false);
                setButtons();
                checkfinish();
            }
        });

        mNextButton = (Button)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
                setButtons();
            }
        });

        mPrevButton = (Button)findViewById(R.id.pre_button);
        mPrevButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex - 1 + mQuestionBank.length) % mQuestionBank.length;
                updateQuestion();
                setButtons();
            }
        });

        updateQuestion();
        setButtons();
        checkfinish();
    }

 

你可能感兴趣的:(《Android编程权威指南(第3版)》:挑战练习)