Back to project page AndroidQuiz.
The source code is released under:
MIT License
If you think the Android project AndroidQuiz listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bignerdranch.android.androidquiz; /* w w w .ja v a2s . c o m*/ import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class CheatActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cheat); boolean mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false); mAnswerTextView = (TextView)findViewById(R.id.answerTextView); recoverState(savedInstanceState); setAnswerShownResult(mCheated); setupButtons(mAnswerIsTrue); } private void setupButtons(final boolean answerIsTrue) { mShowAnswer = (Button)findViewById(R.id.showAnswerButton); mShowAnswer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ mCheated = true; setText(answerIsTrue); setAnswerShownResult(mCheated); } }); } private void recoverState(Bundle savedInstanceState) { if (savedInstanceState != null) { if(savedInstanceState. getInt(BUTTON_PRESSED, 0) != 0) { mCheated = true; setText(mAnswerIsTrue); } } } private void setText(boolean answer) { if(answer) { mAnswerTextView.setText(R.string.true_button); } else { mAnswerTextView.setText(R.string.false_button); } } @Override public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); int i = (mCheated) ? 1 : 0; savedInstanceState.putInt(BUTTON_PRESSED, i); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_cheat, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void setAnswerShownResult(boolean isAnswerShown) { Intent data = new Intent(); data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown); setResult(RESULT_OK, data); } private TextView mAnswerTextView; private Button mShowAnswer; private boolean mCheated = false; private boolean mAnswerIsTrue = false; public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true"; public static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown"; private static final String BUTTON_PRESSED = "pressed"; }