初学安卓——权威指南挑第二版挑战练习

  1.用户作弊后,可以旋转CheatActivity来清除作弊痕迹。

    2.作弊返回后,用户可以旋转QuizActivity来消除mIsCheater变量值。

偷个懒(1和2在资源文件中设置屏幕为横屏,不可旋转)

    android:name=".CheatActivity"
    android:screenOrientation="portrait">

    3.用户可以不断单击NEXT按钮,跳到偷看过答案的问题,从而使作弊记录丢失。


 
  
public class Question {

    private String mQuestion;
    private boolean mAnswerIsTrue;

    public Question(String question, boolean answerIsTrue) {
        mQuestion = question;
        mAnswerIsTrue = answerIsTrue;
    }

    public String getQuestion() {
        return mQuestion;
    }

    public void setQuestion(String question) {
        mQuestion = question;
    }

    public boolean isAnswerIsTrue() {
        return mAnswerIsTrue;
    }

    public void setAnswerIsTrue(boolean answerIsTrue) {
        mAnswerIsTrue = answerIsTrue;
    }
}

QuestionActivity.XML
 
  



    

    

        

QuestionActivity
public class QuestionActivity extends AppCompatActivity {

    @BindView(R.id.question_textview)
    TextView mQuestionTextview;
    @BindView(R.id.true_button)
    Button mTrueButton;
    @BindView(R.id.false_button)
    Button mFalseButton;
    @BindView(R.id.cheat_button)
    Button mCheatButton;
    @BindView(R.id.last_button)
    Button mLastButton;
    @BindView(R.id.next_button)
    Button mNextButton;

    private Question[] mQuestions = new Question[]{
            new Question("it is the first!!!", true),
            new Question("it is the second!!!", false),
            new Question("it is the third!!!", true),
            new Question("it is the four!!!", true),
            new Question("it is the five!!!", false),
            new Question("it is the six!!!", false),
            new Question("it is the seven!!!", true),
    };
    private int mCurrent;//问题索引
    private boolean mIsAnswerTrue;//问题答案是否正确
    private boolean mIsCheat;//是否看过答案
    private int mCLickID;//看过答案的ID
    private List mIntegerList = new ArrayList<>();//保存看过答案的ID

    private static final String EXTRA_ANSWER = "ANSWER";
    private static final String EXTRA_SHOW_ANSWER = "SHOW_ANSWER";
    private static final String EXTRA_QUESTION_ID = "ID";
    private static final String EXTRA_CLICK_ID = "CLICK_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        ButterKnife.bind(this);

        updateQuestion();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 0) {
            if (data == null) {
                return;
            }
            mIsCheat = data.getBooleanExtra(EXTRA_SHOW_ANSWER, false);
            mCLickID = data.getIntExtra(EXTRA_CLICK_ID, 0);

            if ((Integer) mCLickID != null) {
                mIntegerList.add(mCLickID);
            }
        }
    }

    private void checkAnswer(boolean userPressed) {
        setAnswerIsTrue();

        String message;

        if (mIsCheat || mIntegerList.contains(mCurrent)) {
            message = "作弊不好";
        } else {
            if (userPressed == mIsAnswerTrue) {
                message = "答对了";
            } else {
                message = "答错了";
            }
        }
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    private void setAnswerIsTrue() {
        mIsAnswerTrue = mQuestions[mCurrent].isAnswerIsTrue();
    }

    private void previousQuetion() {
        mIsCheat = false;
        if (mCurrent > 0) {
            mCurrent = (mCurrent - 1) % mQuestions.length;
        } else {
            mCurrent = mQuestions.length - 1;
        }
    }

    private void nextQuetion() {
        mIsCheat = false;
        mCurrent = (mCurrent + 1) % mQuestions.length;
    }

    private void updateQuestion() {
        String question = mQuestions[mCurrent].getQuestion();
        mQuestionTextview.setText(question);
    }

    private void openActivity() {
        Intent intent = new Intent(QuestionActivity.this, CheatActivity.class);
        intent.putExtra(EXTRA_ANSWER, mIsAnswerTrue);
        intent.putExtra(EXTRA_QUESTION_ID, mCurrent);
        startActivityForResult(intent, 0);
    }

    @OnClick({R.id.question_textview, R.id.true_button, R.id.false_button, R.id.cheat_button, R.id.last_button, R.id.next_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.question_textview:
                nextQuetion();
                updateQuestion();
                break;
            case R.id.true_button:
                checkAnswer(true);
                break;
            case R.id.false_button:
                checkAnswer(false);
                break;
            case R.id.cheat_button:
                setAnswerIsTrue();
                openActivity();
                break;
            case R.id.last_button:
                previousQuetion();
                updateQuestion();
                break;
            case R.id.next_button:
                nextQuetion();
                updateQuestion();
                break;
        }
    }


}

CheatActivity.XML



    

    

    

CheatActivity
 
  
public class CheatActivity extends AppCompatActivity {

    private static final String EXTRA_ANSWER = "ANSWER";
    private static final String EXTRA_SHOW_ANSWER = "SHOW_ANSWER";
    private static final String EXTRA_QUESTION_ID = "ID";
    private static final String EXTRA_CLICK_ID = "CLICK_ID";


    @BindView(R.id.answer_textview)
    TextView mAnswerTextview;
    @BindView(R.id.show_answer)
    Button mShowAnswer;

    private boolean mIsAnswerTrue;
    private int mQuestionID;
    private int mClickID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        ButterKnife.bind(this);

        mIsAnswerTrue = getIntent().getBooleanExtra(EXTRA_ANSWER, false);
        mQuestionID = getIntent().getIntExtra(EXTRA_QUESTION_ID, 0);
    }


    @OnClick(R.id.show_answer)
    public void onViewClicked() {

        if (mIsAnswerTrue) {
            mAnswerTextview.setText("TRUE");
        } else {
            mAnswerTextview.setText("FALSE");
        }
        setAnswerResult(true);
        mClickID = mQuestionID;
    }

    private void setAnswerResult(boolean isTrue) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_SHOW_ANSWER, isTrue);
        //看过答案传ID
        if (isTrue) {
            intent.putExtra(EXTRA_CLICK_ID, mClickID);
        }
        setResult(RESULT_OK, intent);
    }
}



你可能感兴趣的:(初学安卓——权威指南挑第二版挑战练习)