//CheatActivity.java
package com.bignerdranch.android.geoquiz;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE="com.bignerdranch.android.geoquiz.answer_is_true";
private static final String EXTRA_ANSWER_SHOWN="com.bignerdranch.android.geoquiz.answer_shown";
private boolean mAnswerIsTrue;
private TextView mAnswerTextview;
private Button mShowAnswer;
public static Intent newIntent(Context packageContext,boolean answerIsTrue){
Intent i=new Intent(packageContext,CheatActivity.class);
i.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
return i;
}
public static boolean wasAnswerShown(Intent result){
return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue=getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
mAnswerTextview=(TextView)findViewById(R.id.answer_text_view);
mShowAnswer=(Button)findViewById(R.id.show_answer_button);
mShowAnswer.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(mAnswerIsTrue){
mAnswerTextview.setText(R.string.true_button);
}else{
mAnswerTextview.setText(R.string.false_button);
}
setAnswerShowResult(true);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
int cx = mShowAnswer.getWidth() / 2;
int cy = mShowAnswer.getHeight() / 2;
float radius = mShowAnswer.getWidth();
Animator anim = ViewAnimationUtils.createCircularReveal(mShowAnswer, cx, cy, radius, 0);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mShowAnswer.setVisibility(View.INVISIBLE);
}
});
anim.start();
}else{
mShowAnswer.setVisibility(View.INVISIBLE);
}
}
});
}
private void setAnswerShowResult(boolean isAnserShown){
Intent data=new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN,isAnserShown);
setResult(RESULT_OK,data);
}
}
//Question.java
package com.bignerdranch.android.geoquiz;
public class Question {
private int mTextResId;
private boolean mAnswerTrue;
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 Question(int textResId, boolean answerTrue){
mTextResId=textResId;
mAnswerTrue=answerTrue;
}
}
//QuizActivity
package com.bignerdranch.android.geoquiz;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mLastButton;
private Button mCheatButton;
private TextView mQuestionTextView;
private static final String TAG="QuizActivity";
private static final String KEY_INDEX="index";
private static final int REQUEST_CODE_CHEAT=0;
private Question[] mQuestionBank=new Question[]{
new Question(R.string.question_oceans,true),
new Question(R.string.question_mideast,true),
new Question(R.string.question_africa,true),
new Question(R.string.question_americas,true),
new Question(R.string.question_asia,true),
};
private int mCurrentIndex=0;
private boolean mIsCheater;
private void updateQuestion(){
int question=mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue=mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId=0;
if(mIsCheater){
messageResId=R.string.judgment_toast;
}else {
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreat(Bundle) called");
setContentView(R.layout.activity_quize);//资源ID
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);
}
});
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 view) {
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
mIsCheater=false;
updateQuestion();
}
});
mLastButton=(Button)findViewById(R.id.last_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex=(mCurrentIndex+mQuestionBank.length-1)%mQuestionBank.length;
updateQuestion();
}
});
mCheatButton=(Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Start
boolean answerIsTrue=mQuestionBank[mCurrentIndex].isAnswerTrue();
Intent i=CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
startActivityForResult(i,REQUEST_CODE_CHEAT);
}
});
if(savedInstanceState!=null){
mCurrentIndex=savedInstanceState.getInt(KEY_INDEX,0);
}
updateQuestion();
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(resultCode!= Activity.RESULT_OK){
return;
}
if(requestCode==REQUEST_CODE_CHEAT){
if(data==null){
return;
}
mIsCheater=CheatActivity.wasAnswerShown(data);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG,"onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX,mCurrentIndex);
}
@Override
public void onStart(){
super.onStart();
Log.d(TAG,"onStart() called");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG,"onPause() called");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG,"onResume() called");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG,"onStop() called");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG,"onDestroy() called");
}
}
//strings.xml
<resources>
<string name="app_name">GeoQuiz</string>
<string name="true_button">正确</string>
<string name="false_button">错误</string>
<string name="next_button">下一题</string>
<string name="last_button">上一题</string>
<string name="correct_toast">回答正确</string>
<string name="incorrect_toast">回答错误</string>
<string name="question_oceans">问题1.答案正确</string>
<string name="question_mideast">问题2.....</string>
<string name="question_africa">问题3.....</string>
<string name="question_americas">问题4......</string>
<string name="question_asia">问题5......</string>
<string name="warning_text">确定操作?</string>
<string name="show_answer_button">查看答案</string>
<string name="cheat_button">作弊</string>
<string name="judgment_toast">作弊是不对的</string>
</resources>
//activity_cheat.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.bignerdranch.android.geoquiz.CheatActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/answer_text_view"
android:padding="24dp"
tools:text="Answer"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show_answer_button"
android:text="@string/show_answer_button"/>
</LinearLayout>
//layout\activity_quize.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cheat_button"
android:text="@string/cheat_button"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/last_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/last_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"/>
</LinearLayout>
</LinearLayout>
//land\activity_quize.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="24dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@id/cheat_button"
android:layout_gravity="center_horizontal|bottom"
android:text="@string/cheat_button"/>
<Button
android:id="@+id/last_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:text="@string/last_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:text="@string/next_button"/>
</FrameLayout>