Advertisement
Guest User

MainActivity.java

a guest
Oct 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. package com.example.ragie.sampleactivitytofragment;
  2.  
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11.  
  12. import java.util.Random;
  13.  
  14. public class Main2Activity extends AppCompatActivity {
  15.  
  16.  
  17. Button answer1, answer2, answer3, answer4;
  18. TextView score, question;
  19.  
  20. private Question mQuestion = new Question();
  21. private String mAnswer;
  22. private int mScore = 0;
  23. private int mQuestionLength = mQuestion.mQuestion.length;
  24.  
  25. Random r;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main2);
  31.  
  32. r = new Random();
  33.  
  34. answer1 = (Button) findViewById(R.id.answer1);
  35. answer2 = (Button) findViewById(R.id.answer2);
  36. answer3 = (Button) findViewById(R.id.answer3);
  37. answer4 = (Button) findViewById(R.id.answer4);
  38.  
  39. score = (TextView) findViewById(R.id.score);
  40. question = (TextView) findViewById(R.id.question);
  41. score.setText("Score:" + mScore);
  42.  
  43. updateQuestion(r.nextInt(mQuestionLength));
  44.  
  45. answer1.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. if (answer1.getText() ==mAnswer){
  49. mScore++;
  50. score.setText("Score:" + mScore);
  51. updateQuestion(r.nextInt(mQuestionLength));
  52.  
  53. }else {
  54.  
  55. gameOver();
  56. }
  57.  
  58. }
  59. });
  60.  
  61. answer2.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. if (answer2.getText() ==mAnswer){
  65. mScore++;
  66. score.setText("Score:" + mScore);
  67. updateQuestion(r.nextInt(mQuestionLength));
  68.  
  69. }else {
  70.  
  71. gameOver();
  72.  
  73. }
  74.  
  75. }
  76. });
  77.  
  78. answer3.setOnClickListener(new View.OnClickListener() {
  79. @Override
  80. public void onClick(View v) {
  81. if (answer3.getText() ==mAnswer){
  82. mScore++;
  83. score.setText("Score:" + mScore);
  84. updateQuestion(r.nextInt(mQuestionLength));
  85.  
  86. }else {
  87. gameOver();
  88.  
  89.  
  90. }
  91.  
  92. }
  93. });
  94.  
  95. answer4.setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View v) {
  98. if (answer4.getText() ==mAnswer){
  99. mScore++;
  100. score.setText("Score:" + mScore);
  101. updateQuestion(r.nextInt(mQuestionLength));
  102.  
  103. }else {
  104. gameOver();
  105.  
  106. }}
  107.  
  108. });
  109. }
  110.  
  111.  
  112. private void updateQuestion(int num){
  113. question.setText(mQuestion.getQuestion(num));
  114. answer1.setText(mQuestion.getChoice1(num));
  115. answer2.setText(mQuestion.getChoice2(num));
  116. answer3.setText(mQuestion.getChoice3(num));
  117. answer4.setText(mQuestion.getChoice4(num));
  118.  
  119. mAnswer = mQuestion.getCorrectAnswer(num);
  120.  
  121. }
  122.  
  123. private void gameOver(){
  124. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Main2Activity.this);
  125. alertDialogBuilder
  126. .setMessage("Game Over! Your Score is" + mScore + "points.")
  127. .setCancelable(false)
  128. .setPositiveButton("New Game",
  129. new DialogInterface.OnClickListener() {
  130. @Override
  131. public void onClick(DialogInterface dialog, int i) {
  132. startActivity(new Intent(getApplicationContext(), Main2Activity.class));
  133. finish();
  134.  
  135. }
  136. })
  137. .setNegativeButton ("Exit",
  138. new DialogInterface.OnClickListener() {
  139. @Override
  140. public void onClick(DialogInterface dialog, int i) {
  141. finish();
  142.  
  143. }
  144. });
  145. AlertDialog alertDialog = alertDialogBuilder.create();
  146. alertDialog.show();
  147.  
  148. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement