Guest User

Untitled

a guest
Feb 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.37 KB | None | 0 0
  1. package com.example.android.htmlquiz;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.EditText;
  10. import android.widget.LinearLayout;
  11. import android.widget.RadioButton;
  12. import android.widget.RadioGroup;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. int step = 1;
  19. int correctAnswer = 0;
  20. int wrongAnswer = 0;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. }
  27.  
  28. /**
  29. * Start the quiz showing the first question
  30. * @param v
  31. */
  32. public void startQuiz(View v){
  33. LinearLayout one = (LinearLayout) findViewById(R.id.start_quiz);
  34. one.setVisibility(View.GONE);
  35. LinearLayout question_1 = (LinearLayout) findViewById(R.id.question_1);
  36. question_1.setVisibility(View.VISIBLE);
  37.  
  38. }
  39.  
  40. /**
  41. * Check for answers, increase counters and go to next step
  42. * @param v
  43. */
  44. public void checkQuiz(View v){
  45.  
  46.  
  47. switch(step) {
  48. case 1:
  49. RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group_q1);
  50. if (radioGroup.getCheckedRadioButtonId() == -1) {
  51. // no radio buttons are checked
  52. }
  53. else {
  54. // one of the radio buttons is checked
  55. int selectedId = radioGroup.getCheckedRadioButtonId();
  56. RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
  57. String answer = selectedRadioButton.getText().toString();
  58. if(answer.equals("Both of the above")) {
  59. correctAnswer += 1;
  60. } else {
  61. wrongAnswer += 1;
  62. }
  63.  
  64. LinearLayout question_1 = (LinearLayout) findViewById(R.id.question_1);
  65. question_1.setVisibility(View.GONE);
  66. LinearLayout question_2 = (LinearLayout) findViewById(R.id.question_2);
  67. question_2.setVisibility(View.VISIBLE);
  68. step += 1;
  69. }
  70. break;
  71. case 2:
  72. RadioGroup radioGroup2 = (RadioGroup) findViewById(R.id.radio_group_q2);
  73. if (radioGroup2.getCheckedRadioButtonId() == -1) {
  74. // no radio buttons are checked
  75. }
  76. else {
  77. // one of the radio buttons is checked
  78. int selectedId = radioGroup2.getCheckedRadioButtonId();
  79. RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
  80. String answer = selectedRadioButton.getText().toString();
  81. if(answer.equals("All of the above")) {
  82. correctAnswer += 1;
  83. } else {
  84. wrongAnswer += 1;
  85. }
  86.  
  87. LinearLayout question_2 = (LinearLayout) findViewById(R.id.question_2);
  88. question_2.setVisibility(View.GONE);
  89. LinearLayout question_3 = (LinearLayout) findViewById(R.id.question_3);
  90. question_3.setVisibility(View.VISIBLE);
  91. step += 1;
  92. }
  93. break;
  94. case 3:
  95. RadioGroup radioGroup3 = (RadioGroup) findViewById(R.id.radio_group_q3);
  96. if (radioGroup3.getCheckedRadioButtonId() == -1) {
  97. // no radio buttons are checked
  98. }
  99. else {
  100. // one of the radio buttons is checked
  101. int selectedId = radioGroup3.getCheckedRadioButtonId();
  102. RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
  103. String answer = selectedRadioButton.getText().toString();
  104. if(answer.equals("date")) {
  105. correctAnswer += 1;
  106. } else {
  107. wrongAnswer += 1;
  108. }
  109.  
  110. LinearLayout question_3 = (LinearLayout) findViewById(R.id.question_3);
  111. question_3.setVisibility(View.GONE);
  112. LinearLayout question_4 = (LinearLayout) findViewById(R.id.question_4);
  113. question_4.setVisibility(View.VISIBLE);
  114. step += 1;
  115. }
  116. break;
  117. case 4:
  118. RadioGroup radioGroup4 = (RadioGroup) findViewById(R.id.radio_group_q4);
  119. if (radioGroup4.getCheckedRadioButtonId() == -1) {
  120. // no radio buttons are checked
  121. }
  122. else {
  123. // one of the radio buttons is checked
  124. int selectedId = radioGroup4.getCheckedRadioButtonId();
  125. RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
  126. String answer = selectedRadioButton.getText().toString();
  127. if(answer.equals("All of the above")) {
  128. correctAnswer += 1;
  129. } else {
  130. wrongAnswer += 1;
  131. }
  132.  
  133. LinearLayout question_4 = (LinearLayout) findViewById(R.id.question_4);
  134. question_4.setVisibility(View.GONE);
  135. LinearLayout question_5 = (LinearLayout) findViewById(R.id.question_5);
  136. question_5.setVisibility(View.VISIBLE);
  137. step += 1;
  138. }
  139. break;
  140. case 5:
  141. RadioGroup radioGroup5 = (RadioGroup) findViewById(R.id.radio_group_q5);
  142. if (radioGroup5.getCheckedRadioButtonId() == -1) {
  143. // no radio buttons are checked
  144. }
  145. else {
  146. // one of the radio buttons is checked
  147. int selectedId = radioGroup5.getCheckedRadioButtonId();
  148. RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
  149. String answer = selectedRadioButton.getText().toString();
  150. if(answer.equals("All of the above")) {
  151. correctAnswer += 1;
  152. } else {
  153. wrongAnswer += 1;
  154. }
  155.  
  156. LinearLayout question_5 = (LinearLayout) findViewById(R.id.question_5);
  157. question_5.setVisibility(View.GONE);
  158. LinearLayout question_6 = (LinearLayout) findViewById(R.id.question_6);
  159. question_6.setVisibility(View.VISIBLE);
  160. step += 1;
  161. }
  162. break;
  163. case 6:
  164. RadioGroup radioGroup6 = (RadioGroup) findViewById(R.id.radio_group_q6);
  165. if (radioGroup6.getCheckedRadioButtonId() == -1) {
  166. // no radio buttons are checked
  167. }
  168. else {
  169. // one of the radio buttons is checked
  170. int selectedId = radioGroup6.getCheckedRadioButtonId();
  171. RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
  172. String answer = selectedRadioButton.getText().toString();
  173. if(answer.equals("geolocation.getCurrentPosition()")) {
  174. correctAnswer += 1;
  175. } else {
  176. wrongAnswer += 1;
  177. }
  178.  
  179. LinearLayout question_6 = (LinearLayout) findViewById(R.id.question_6);
  180. question_6.setVisibility(View.GONE);
  181. LinearLayout question_7 = (LinearLayout) findViewById(R.id.question_7);
  182. question_7.setVisibility(View.VISIBLE);
  183.  
  184. step += 1;
  185. }
  186. break;
  187. case 7:
  188. CheckBox a71 = (CheckBox) findViewById(R.id.a71);
  189. CheckBox a72 = (CheckBox) findViewById(R.id.a72);
  190. CheckBox a73 = (CheckBox) findViewById(R.id.a73);
  191. CheckBox a74 = (CheckBox) findViewById(R.id.a74);
  192. if(a71.isChecked() && a72.isChecked() && a74.isChecked()){
  193. correctAnswer += 1;
  194. } else {
  195. wrongAnswer += 1;
  196. }
  197.  
  198. LinearLayout question_7 = (LinearLayout) findViewById(R.id.question_7);
  199. question_7.setVisibility(View.GONE);
  200. LinearLayout question_8 = (LinearLayout) findViewById(R.id.question_8);
  201. question_8.setVisibility(View.VISIBLE);
  202. step += 1;
  203. break;
  204. case 8:
  205. EditText et = (EditText) findViewById(R.id.missingTag);
  206. String answer = et.getText().toString();
  207. if(answer.equals("section")){
  208. correctAnswer += 1;
  209. } else {
  210. wrongAnswer += 1;
  211. }
  212. LinearLayout question_9 = (LinearLayout) findViewById(R.id.question_8);
  213. question_9.setVisibility(View.GONE);
  214.  
  215. TextView success = (TextView) findViewById(R.id.success);
  216. success.setText("Correct answers: " + correctAnswer);
  217.  
  218. TextView text_result = (TextView) findViewById(R.id.text_result);
  219. text_result.setText("Your final score is: " + correctAnswer);
  220.  
  221.  
  222. TextView wrong = (TextView) findViewById(R.id.wrong);
  223. wrong.setText("Wrong answers: " + wrongAnswer);
  224.  
  225. LinearLayout final_result = (LinearLayout) findViewById(R.id.final_result);
  226. final_result.setVisibility(View.VISIBLE);
  227.  
  228. Toast.makeText(getApplicationContext(), "Your finale score is; " + correctAnswer, Toast.LENGTH_LONG).show();
  229.  
  230.  
  231. step += 1;
  232. break;
  233. default:
  234. //setContentView(R.layout.default);
  235. }
  236.  
  237.  
  238. }
  239.  
  240. /**
  241. * Reset status anc counters
  242. * @param v
  243. */
  244. public void restartQuiz(View v){
  245. /* Reset counters */
  246. this.step = 1;
  247. this.correctAnswer = 0;
  248. this.wrongAnswer = 0;
  249.  
  250. /* Reset button status */
  251. RadioButton rb11 = (RadioButton) findViewById(R.id.a11);
  252. rb11.setChecked(true);
  253.  
  254. RadioButton rb21 = (RadioButton) findViewById(R.id.a21);
  255. rb21.setChecked(true);
  256.  
  257. RadioButton rb31 = (RadioButton) findViewById(R.id.a31);
  258. rb31.setChecked(true);
  259.  
  260. RadioButton rb41 = (RadioButton) findViewById(R.id.a41);
  261. rb41.setChecked(true);
  262.  
  263. CheckBox cb71 = (CheckBox) findViewById(R.id.a71);
  264. cb71.setChecked(false);
  265.  
  266. CheckBox cb72 = (CheckBox) findViewById(R.id.a72);
  267. cb71.setChecked(false);
  268.  
  269. CheckBox cb73 = (CheckBox) findViewById(R.id.a73);
  270. cb71.setChecked(false);
  271.  
  272. CheckBox cb74 = (CheckBox) findViewById(R.id.a74);
  273. cb71.setChecked(false);
  274.  
  275. EditText et = findViewById(R.id.missingTag);
  276. et.setText("");
  277.  
  278. /* Hide results, show first step */
  279. LinearLayout one = (LinearLayout) findViewById(R.id.start_quiz);
  280. one.setVisibility(View.VISIBLE);
  281. LinearLayout final_result = (LinearLayout) findViewById(R.id.final_result);
  282. final_result.setVisibility(View.GONE);
  283. }
  284.  
  285.  
  286. }
Add Comment
Please, Sign In to add comment