Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mamaril.justin.dronelawquiz;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.w3c.dom.Text;
- public class MainActivity extends AppCompatActivity {
- private int score = 0;
- private int tries = 0;
- // 1. Declare your views as class variables
- Button submitAnswersButton;
- Button resetQuizButton;
- RadioGroup question1;
- RadioGroup question2;
- RadioGroup question3;
- RadioGroup question4;
- RadioGroup question5;
- RadioGroup question6;
- RadioGroup question7;
- RadioGroup question8;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 2. Initialize your views onCreate, after inflating the Layout
- submitAnswersButton = (Button) findViewById(R.id.submit_answers_button);
- resetQuizButton = (Button) findViewById(R.id.reset_quiz_button);
- question1 = (RadioGroup) findViewById(R.id.question_1);
- question2 = (RadioGroup) findViewById(R.id.question_2);
- question3 = (RadioGroup) findViewById(R.id.question_3);
- question4 = (RadioGroup) findViewById(R.id.question_4);
- question5 = (RadioGroup) findViewById(R.id.question_5);
- question6 = (RadioGroup) findViewById(R.id.question_6);
- question7 = (RadioGroup) findViewById(R.id.question_7);
- question8 = (RadioGroup) findViewById(R.id.question_8);
- // 3. Run your setOnClickListener methods without those pesky null pointer warnings
- submitAnswersButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- calculateScore();
- Toast.makeText(MainActivity.this, "Score: " + score + "/8. Hit 'Reset Quiz' to try again!", Toast.LENGTH_LONG).show();
- }
- });
- resetQuizButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- resetQuiz();
- }
- });
- }
- private void calculateScore() {
- if (tries == 0) {
- // 4. Run your getCheckedRadioButtonId methods without those pesky null pointer warnings
- int one = question1.getCheckedRadioButtonId();
- int two = question2.getCheckedRadioButtonId();
- int three = question3.getCheckedRadioButtonId();
- int four = question4.getCheckedRadioButtonId();
- int five = question5.getCheckedRadioButtonId();
- int six = question6.getCheckedRadioButtonId();
- int seven = question7.getCheckedRadioButtonId();
- int eight = question8.getCheckedRadioButtonId();
- if (one == 1)
- score += 1;
- if (two == 5)
- score += 1;
- if (three == 7)
- score += 1;
- if (four == 12)
- score += 1;
- if (five == 14)
- score += 1;
- if (six == 16)
- score += 1;
- if (seven == 19)
- score += 1;
- if (eight == 24)
- score += 1;
- tries = 1;
- }
- }
- private void resetQuiz() {
- score = 0;
- tries = 0;
- // 5. Run your clearCheck methods without those pesky null pointer warnings
- question1.clearCheck();
- question2.clearCheck();
- question3.clearCheck();
- question4.clearCheck();
- question5.clearCheck();
- question6.clearCheck();
- question7.clearCheck();
- question8.clearCheck();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement