Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.quizapp;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- int score = 0;
- String answer1 = "Random Access Memory";
- String answer4 = "1000";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void answerQuestion1() {
- EditText answerFor1 = (EditText) findViewById(R.id.question1_answer);
- String answerQ1 = answerFor1.getText().toString();
- if (answerQ1 == answer1){
- score += 1;
- }
- }
- public void answerQuestion2(){
- CheckBox checkAlu = (CheckBox) findViewById(R.id.question2_answer1);
- boolean checkedAlu = checkAlu.isChecked();
- CheckBox checkRam = (CheckBox) findViewById(R.id.question2_answer2);
- boolean checkedRam = checkAlu.isChecked();
- CheckBox checkRegisters = (CheckBox) findViewById(R.id.question2_answer3);
- boolean checkedRegisters = checkRegisters.isChecked();
- if(checkedAlu == false && checkedRam == true && checkedRegisters == false){
- score += 1;
- }
- else {
- score += 0;
- }
- }
- public void onRadioButtonClickedQ3(View view) {
- // Is the button now checked?
- boolean checked = ((RadioButton) view).isChecked();
- // Check which radio button was clicked
- switch(view.getId()) {
- case R.id.question3_answer1:
- if (checked)
- score += 1;
- break;
- case R.id.question3_answer2:
- if (checked)
- score += 0;
- break;
- }
- }
- public void answerQuestion4(){
- EditText answerFor4 = (EditText) findViewById(R.id.question4_answer);
- String answerQ4 = answerFor4.getText().toString();
- if (answerQ4 == answer4){
- score += 1;
- }
- }
- public void onRadioButtonClickedQ5(View view) {
- // Is the button now checked?
- boolean checked = ((RadioButton) view).isChecked();
- // Check which radio button was clicked
- switch(view.getId()) {
- case R.id.question5_answer2:
- if (checked)
- score += 1;
- break;
- case R.id.question5_answer1:
- if (checked)
- score += 0;
- break;
- }
- }
- public void submitAnswers(View view){
- answerQuestion1();
- answerQuestion2();
- onRadioButtonClickedQ3();
- answerQuestion4();
- onRadioButtonClickedQ5();
- Toast.makeText(this, score + " out of 5", Toast.LENGTH_LONG).show();
- }
- }
Add Comment
Please, Sign In to add comment