Created
March 21, 2019 02:39
-
-
Save xSurpassionx/b945c69d77f066fe4ba29309adbdd935 to your computer and use it in GitHub Desktop.
For My Java Quiz App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package android.example.sharkquiz; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
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 { | |
private int score = 0; | |
private RadioGroup questionOneTf; | |
private CheckBox checkBox1; | |
private CheckBox checkBox2; | |
private CheckBox checkBox3; | |
private RadioGroup questionThreeTf; | |
private EditText sharkAnswer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//When True or False is Selected for question 1 | |
questionOneTf = (RadioGroup) findViewById(R.id.question_one_true_false_radio_group); | |
questionOneTf.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup group, int checkedId) { | |
if(checkedId== R.id.question_one_true) | |
score++; | |
else | |
score--; | |
} | |
}); | |
boolean questionOneTrue = ((RadioButton) findViewById(R.id.question_one_true)).isChecked(); | |
//When Answers are checked for question 2 | |
checkBox1 = (CheckBox) findViewById(R.id.question_two_box_1); | |
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if(isChecked) | |
score++; | |
else | |
score--; | |
} | |
}); | |
// boolean sharksHearing = checkBox1.isChecked(); | |
checkBox2 = (CheckBox) findViewById(R.id.question_two_box_2); | |
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if(isChecked) | |
score++; | |
else | |
score--; | |
} | |
}); | |
checkBox3 = (CheckBox) findViewById(R.id.question_two_box_3); | |
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if(isChecked) | |
score++; | |
else | |
score--; | |
} | |
}); | |
//When true or false is selected for Question 3 | |
questionThreeTf = (RadioGroup) findViewById(R.id.question_three_true_false_radio_group); | |
questionThreeTf.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup group, int checkedId) { | |
if(checkedId== R.id.question_three_true) | |
score++; | |
else | |
score--; | |
} | |
}); | |
sharkAnswer = (EditText) findViewById(R.id.largest_shark); | |
} | |
//This method is called when the Submit Quiz button is selected | |
public void submitQuiz(View view) { | |
if(sharkAnswer.getText().toString().equals("Whale Shark")) | |
score++; | |
Toast.makeText(this, "SCORE "+score, Toast.LENGTH_LONG).show(); | |
clear(); | |
} | |
private void clear(){ | |
checkBox1.setChecked(false); | |
checkBox2.setChecked(false); | |
checkBox3.setChecked(false); | |
questionOneTf.clearCheck(); | |
questionThreeTf.clearCheck(); | |
sharkAnswer.setText(null); | |
score=0; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment