MainActivity:
package com.example.my_app;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private ImageButton mBackButton;
private Button mCheatButton;
private TextView mQuestionTextView;
private static final String TAG = "MainActivity";
private static final String KEY_INDEX = "index";
private static final String Is_Cheat = "cheat";
private static final String CHOOSE = "choose";
private static final String FINISH = "finish";
private static final String RIGHT = "right";
private static final String WRONG = "wrong";
private static final String CHEAT_Q = "cheating";
private static final int REQUEST_CODE_CHEAT = 0;
private static final int REQUEST_CODE_COMMAND = 1;
private int mCurrentIndex = 0;
private int right_q = 0;
private int wrong_q = 0;
private int cheat_q = 0;
private int command = 0;
private int finish = 0;
private boolean[] mCheat = new boolean[5];
private int[] mChoose = new int[]{0,0,0,0,0};
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_americas, true),
new Question(R.string.question_asia, true),
};
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(mCheat[mCurrentIndex]){
messageResid = R.string.judgment_toast;
}else{
if (userPressedTrue == answerIsTrue) {
messageResid = R.string.correct_toast;
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.GREEN);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.GREEN);
}
right_q++;
mChoose[mCurrentIndex] = 1;
} else {
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.RED);
}else{
mTrueButton.setBackgroundColor(Color.RED);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
messageResid = R.string.incorrect_toast;
wrong_q++;
mChoose[mCurrentIndex] = 2;
}
finish++;
}
Toast.makeText(this, messageResid, Toast.LENGTH_SHORT).show();
}
@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;
}
mCheat[mCurrentIndex] = CheatActivity.wasAnswerShown(data);
cheat_q++;
finish++;
}
if(requestCode == REQUEST_CODE_COMMAND){
if(data == null){
return;
}
command = ResultActivity.wasCommand(data);
System.out.println(command);
if(command == 1){
command = 0;
finish();
}else if(command == 2){
command = 0;
for(int i = 0; i < 5; i++){
mCheat[i] = false;
mChoose[i] = 0;
}
mCurrentIndex = 0;
updateQuestion();
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.LTGRAY);
right_q = wrong_q = cheat_q = 0;
finish = 0;
}
}
}
public boolean judge_Repeat(){
if(mChoose[mCurrentIndex] != 0){
int repeat_id = R.string.repeat;
Toast.makeText(this, repeat_id, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_main);
System.out.println(command + " 2222");
for(int i = 0; i < 5; i++){
mCheat[i] = false;
}
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
if(mChoose[mCurrentIndex] == 1){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.GREEN);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.GREEN);
}
}else if(mChoose[mCurrentIndex] == 2){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.RED);
}else{
mTrueButton.setBackgroundColor(Color.RED);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}
});
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(judge_Repeat() == true){
return;
}
checkAnswer(true);
if(finish == 5) {
Intent i = ResultActivity.newIntent(MainActivity.this, right_q, wrong_q, cheat_q);
startActivityForResult(i, REQUEST_CODE_COMMAND);
}
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(judge_Repeat() == true){
return;
}
checkAnswer(false);
if(finish == 5) {
Intent i = ResultActivity.newIntent(MainActivity.this, right_q, wrong_q, cheat_q);
startActivityForResult(i, REQUEST_CODE_COMMAND);
}
}
});
mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
if(mChoose[mCurrentIndex] == 1){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.GREEN);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.GREEN);
}
}else if(mChoose[mCurrentIndex] == 2){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.RED);
}else{
mTrueButton.setBackgroundColor(Color.RED);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}
});
mBackButton = (ImageButton) findViewById(R.id.back_button);
mBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mCurrentIndex == 0){
mCurrentIndex = mQuestionBank.length;
}
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
if(mChoose[mCurrentIndex] == 1){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.GREEN);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.GREEN);
}
}else if(mChoose[mCurrentIndex] == 2){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.RED);
}else{
mTrueButton.setBackgroundColor(Color.RED);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}
});
mCheatButton = (Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
Intent i = CheatActivity.newIntent(MainActivity.this, answerIsTrue);
startActivityForResult(i, REQUEST_CODE_CHEAT);
}
});
if(savedInstanceState != null) {
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
mChoose = savedInstanceState.getIntArray(CHOOSE);
finish = savedInstanceState.getInt(FINISH);
right_q = savedInstanceState.getInt(RIGHT);
wrong_q = savedInstanceState.getInt(WRONG);
cheat_q = savedInstanceState.getInt(CHEAT_Q);
mCheat = savedInstanceState.getBooleanArray(Is_Cheat);
}
if(mChoose[mCurrentIndex] == 1){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.GREEN);
mFalseButton.setBackgroundColor(Color.GRAY);
}else{
mTrueButton.setBackgroundColor(Color.GRAY);
mFalseButton.setBackgroundColor(Color.GREEN);
}
}else if(mChoose[mCurrentIndex] == 2){
if(mQuestionBank[mCurrentIndex].isAnswerTrue() == true) {
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.RED);
}else{
mTrueButton.setBackgroundColor(Color.RED);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
}else{
mTrueButton.setBackgroundColor(Color.LTGRAY);
mFalseButton.setBackgroundColor(Color.LTGRAY);
}
updateQuestion();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
savedInstanceState.putBooleanArray(Is_Cheat, mCheat);
savedInstanceState.putIntArray(CHOOSE, mChoose);
savedInstanceState.putInt(FINISH, finish);
savedInstanceState.putInt(RIGHT, right_q);
savedInstanceState.putInt(WRONG, wrong_q);
savedInstanceState.putInt(CHEAT_Q, cheat_q);
}
@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");
}
}
CheatActivity:
package com.example.my_app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE = "com.example.my_app.answer_is_true";
private static final String EXTRA_ANSWER_SHOWN = "com.example.my_app.answer_shown";
private static final String Is_Cheat = "cheat";
private static final String TAG = "CheatActivity";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private int mNowState = 0;
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 void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
public static boolean wasAnswerShown(Intent result) {
return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putInt(Is_Cheat, mNowState);
}
@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 view) {
mNowState = 1;
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}
});
if(savedInstanceState != null){
mNowState = savedInstanceState.getInt(Is_Cheat, 0);
}
if(mNowState == 1){
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}
}
}
ResultActivity:
package com.example.my_app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class ResultActivity extends AppCompatActivity {
private static final String EXTRA_RIGHT_Q = "com.example.my_app.right_q";
private static final String EXTRA_WRONG_Q = "com.example.my_app.wrong_q";
private static final String EXTRA_CHEAT_Q = "com.example.my_app.cheat_q";
private static final String EXTRA_COMMAND = "com.example.my_app.command";
private int right_q, wrong_q, cheat_q;
private Button mQuit;
private Button mRestart;
private TextView mTextCongratulation;
private TextView mTextResult;
private TextView mTextRight;
private TextView mTextWrong;
private TextView mTextCheat;
public static Intent newIntent(Context packageContext, int right_q, int wrong_q, int cheat_q) {
Intent i = new Intent(packageContext, ResultActivity.class);
i.putExtra(EXTRA_RIGHT_Q, right_q);
i.putExtra(EXTRA_WRONG_Q, wrong_q);
i.putExtra(EXTRA_CHEAT_Q, cheat_q);
return i;
}
public void check() {
if(right_q == 5){
mTextCongratulation = (TextView) findViewById(R.id.congratulation);
mTextCongratulation.setText(R.string.congratulation);
}else{
mTextResult = (TextView) findViewById(R.id.result);
mTextResult.setText(R.string.result);
mTextRight = (TextView) findViewById(R.id.right_q);
String s = String.valueOf(right_q);
mTextRight.setText(R.string.right_q );
mTextRight.append(s);
mTextWrong = (TextView) findViewById(R.id.wrong_q);
s = String.valueOf(wrong_q);
mTextWrong.setText(R.string.wrong_q );
mTextWrong.append(s);
mTextCheat = (TextView) findViewById(R.id.cheat_q);
s = String.valueOf(cheat_q);
mTextCheat.setText(R.string.cheat_q);
mTextCheat.append(s);
}
}
private void setCommand(int command){
Intent data = new Intent();
data.putExtra(EXTRA_COMMAND, command);
setResult(RESULT_OK, data);
}
public static int wasCommand(Intent result){
return result.getIntExtra(EXTRA_COMMAND, 0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
right_q = getIntent().getIntExtra(EXTRA_RIGHT_Q, 0);
wrong_q = getIntent().getIntExtra(EXTRA_WRONG_Q, 0);
cheat_q = getIntent().getIntExtra(EXTRA_CHEAT_Q, 0);
check();
mQuit = (Button) findViewById(R.id.quit);
mQuit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setCommand(1);
finish();
}
});
mRestart = (Button) findViewById(R.id.restart);
mRestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setCommand(2);
finish();
}
});
}
}
Question:
package com.example.my_app;
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;
}
}