Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.schumiquiz;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- public class MainActivity extends AppCompatActivity {
- double score = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void reset(View v) {
- score = 0;
- CheckBox q21 = (CheckBox) findViewById(R.id.q21);
- q21.setChecked(false);
- CheckBox q22 = (CheckBox) findViewById(R.id.q22);
- q22.setChecked(false);
- CheckBox q23 = (CheckBox) findViewById(R.id.q23);
- q23.setChecked(false);
- CheckBox q24 = (CheckBox) findViewById(R.id.q24);
- q24.setChecked(false);
- CheckBox q31 = (CheckBox) findViewById(R.id.q31);
- q31.setChecked(false);
- CheckBox q32 = (CheckBox) findViewById(R.id.q32);
- q32.setChecked(false);
- CheckBox q33 = (CheckBox) findViewById(R.id.q33);
- q33.setChecked(false);
- CheckBox q34 = (CheckBox) findViewById(R.id.q34);
- q34.setChecked(false);
- RadioGroup q4 = findViewById(R.id.q4Options);
- q4.clearCheck();
- EditText nameInput = findViewById(R.id.q5Input);
- nameInput.setText(null);
- RadioGroup q1 = findViewById(R.id.q1Options);
- q1.clearCheck();
- }
- public void calculate(View view) {
- // Check the radio button for the first question
- RadioButton rb1 = findViewById(R.id.q13);
- if (rb1.isChecked()) {
- score += 1;
- }
- // Log.v("MainActivity", "Score after 1 question: " + Double.toString(score));
- // Check checkboxes for the second question
- CheckBox q21 = findViewById(R.id.q21);
- boolean q21Checked = q21.isChecked();
- CheckBox q22 = findViewById(R.id.q22);
- boolean q22Checked = q22.isChecked();
- CheckBox q23 = findViewById(R.id.q23);
- boolean q23Checked = q23.isChecked();
- CheckBox q24 = findViewById(R.id.q24);
- boolean q24Checked = q24.isChecked();
- // Check answers to second question (multiple choice)
- if (!q21Checked && !q22Checked && !q23Checked && !q24Checked) {
- //if no answer was selected, it is counted as zero points
- } else {
- if (q21Checked) {
- score += 0.25;
- }
- if (!q22Checked) {
- score += 0.25;
- }
- if (q23Checked) {
- score += 0.25;
- }
- if (!q24Checked) {
- score += 0.25;
- }
- }
- // Log.v("MainActivity", "Score after 2 questions: " + Double.toString(score));
- // Check checkboxes for the third question
- CheckBox q31 = findViewById(R.id.q31);
- boolean q31Checked = q31.isChecked();
- CheckBox q32 = findViewById(R.id.q32);
- boolean q32Checked = q32.isChecked();
- CheckBox q33 = findViewById(R.id.q33);
- boolean q33Checked = q33.isChecked();
- CheckBox q34 = findViewById(R.id.q34);
- boolean q34Checked = q34.isChecked();
- // Same logic for points calculation as with previous question
- if (!q31Checked && !q32Checked && !q33Checked && !q34Checked) {
- //if no answer was selected, it is counted as zero points
- } else {
- if (!q31Checked) {
- score += 0.25;
- }
- if (!q32Checked) {
- score += 0.25;
- }
- if (q33Checked) {
- score += 0.25;
- }
- if (!q34Checked) {
- score += 0.25;
- }
- }
- //Log.v("MainActivity", "Score after 3 questions: " + Double.toString(score));
- // Radio button for the fourth question is checked
- RadioButton rb4 = findViewById(R.id.q44);
- if (rb4.isChecked()) {
- score += 1;
- }
- //Log.v("MainActivity", "Score after 4 questions: " + Double.toString(score));
- // Text input in question five is checked
- EditText nameInput = findViewById(R.id.q5Input);
- String q5Input = nameInput.getText().toString();
- if (q5Input.equals("Corinna")) {
- score += 1;
- }
- //Log.v("MainActivity", "Score after 5 questions: " + Double.toString(score));
- // Toast displayed
- if (score == 5) {
- Toast.makeText(this, "Congratulations! All answers are correct!", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(this, "Your score is " + Double.toString(score) + " points out of 5", Toast.LENGTH_SHORT).show();
- }
- score = 0;
- //Log.v("MainActivity", "Score after Toast: " + Double.toString(score));
- }
- }
Add Comment
Please, Sign In to add comment