Guest User

Untitled

a guest
Jan 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package com.example.android.quizapp;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.CheckBox;
  8. import android.widget.EditText;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15. int score = 0;
  16. String answer1 = "Random Access Memory";
  17. String answer4 = "1000";
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24.  
  25.  
  26. }
  27.  
  28. public void answerQuestion1() {
  29.  
  30. EditText answerFor1 = (EditText) findViewById(R.id.question1_answer);
  31. String answerQ1 = answerFor1.getText().toString();
  32.  
  33. if (answerQ1 == answer1){
  34. score += 1;
  35. }
  36.  
  37. }
  38. public void answerQuestion2(){
  39. CheckBox checkAlu = (CheckBox) findViewById(R.id.question2_answer1);
  40. boolean checkedAlu = checkAlu.isChecked();
  41.  
  42. CheckBox checkRam = (CheckBox) findViewById(R.id.question2_answer2);
  43. boolean checkedRam = checkAlu.isChecked();
  44.  
  45. CheckBox checkRegisters = (CheckBox) findViewById(R.id.question2_answer3);
  46. boolean checkedRegisters = checkRegisters.isChecked();
  47.  
  48. if(checkedAlu == false && checkedRam == true && checkedRegisters == false){
  49. score += 1;
  50. }
  51. else {
  52. score += 0;
  53. }
  54. }
  55. public void onRadioButtonClickedQ3(View view) {
  56. // Is the button now checked?
  57. boolean checked = ((RadioButton) view).isChecked();
  58.  
  59. // Check which radio button was clicked
  60. switch(view.getId()) {
  61. case R.id.question3_answer1:
  62. if (checked)
  63. score += 1;
  64. break;
  65. case R.id.question3_answer2:
  66. if (checked)
  67. score += 0;
  68. break;
  69. }
  70. }
  71.  
  72. public void answerQuestion4(){
  73. EditText answerFor4 = (EditText) findViewById(R.id.question4_answer);
  74. String answerQ4 = answerFor4.getText().toString();
  75.  
  76. if (answerQ4 == answer4){
  77. score += 1;
  78. }
  79. }
  80. public void onRadioButtonClickedQ5(View view) {
  81. // Is the button now checked?
  82. boolean checked = ((RadioButton) view).isChecked();
  83.  
  84. // Check which radio button was clicked
  85. switch(view.getId()) {
  86. case R.id.question5_answer2:
  87. if (checked)
  88. score += 1;
  89. break;
  90. case R.id.question5_answer1:
  91. if (checked)
  92. score += 0;
  93. break;
  94. }
  95. }
  96.  
  97.  
  98. public void submitAnswers(View view){
  99. answerQuestion1();
  100. answerQuestion2();
  101. onRadioButtonClickedQ3();
  102. answerQuestion4();
  103. onRadioButtonClickedQ5();
  104.  
  105. Toast.makeText(this, score + " out of 5", Toast.LENGTH_LONG).show();
  106.  
  107. }
  108. }
Add Comment
Please, Sign In to add comment