Created
August 5, 2016 15:59
-
-
Save RowlandOti/b0b4801199afb515d6e8bccfce27641d to your computer and use it in GitHub Desktop.
A Quiz Activity with Scores from CheckBoxex, RadioButtons and EditText
This file contains hidden or 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 com.example.android.quizz; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
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; | |
public class MainActivity extends AppCompatActivity { | |
private static int score = 0; | |
private RadioGroup radioGroupAnimal; | |
private EditText editTextContinent; | |
private CheckBox chkBxNileRiver; | |
private CheckBox chkBxZambeziRiver; | |
private CheckBox chkBxNepalRiver; | |
private boolean isChoiceMammal = false; | |
private boolean isChoiceNile = false; | |
private CompoundButton.OnCheckedChangeListener chkCheckedListner; | |
private RadioGroup.OnCheckedChangeListener radioCheckChangeListener; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Initialize the CheckBoxes | |
chkBxNileRiver = (CheckBox) findViewById(R.id.nileriver); | |
chkBxZambeziRiver = (CheckBox) findViewById(R.id.zambeziriver); | |
chkBxNepalRiver = (CheckBox) findViewById(R.id.nepalriver); | |
// Initialize EditText | |
editTextContinent = (EditText) findViewById(R.id.et_continent); | |
// Initialize the RadioGroup | |
radioGroupAnimal = (RadioGroup) findViewById(R.id.animal); | |
setListeners(); | |
} | |
public void setListeners() { | |
// CheckBox listeners | |
chkCheckedListner = new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton selectedCheckBox, boolean isChecked) { | |
// Check which checkbox was clicked | |
switch (selectedCheckBox.getId()) { | |
case R.id.nileriver: | |
if (isChecked) { | |
isChoiceNile = true; | |
} | |
break; | |
case R.id.zambeziriver: | |
if (isChecked) { | |
isChoiceNile = false; | |
} | |
break; | |
case R.id.nepalriver: | |
if (isChecked) { | |
isChoiceNile = false; | |
} | |
break; | |
} | |
} | |
}; | |
chkBxNileRiver.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxZambeziRiver.setOnCheckedChangeListener(chkCheckedListner); | |
chkBxNepalRiver.setOnCheckedChangeListener(chkCheckedListner); | |
// RadioGroup Listeners | |
radioCheckChangeListener = new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) { | |
switch (radioGroup.getId()) { | |
case R.id.animal: | |
if (R.id.radio_mammal == radioButtonId) { | |
isChoiceMammal = false; | |
} | |
if (R.id.radio_reptile == radioButtonId) { | |
isChoiceMammal = true; | |
} | |
break; | |
} | |
} | |
}; | |
radioGroupAnimal.setOnCheckedChangeListener(radioCheckChangeListener); | |
} | |
public void display(String message) { | |
TextView ScoreView = (TextView) findViewById(R.id.socre_view); | |
ScoreView.setText(message); | |
ScoreView.setHighlightColor(Color.YELLOW); | |
} | |
public void submit(View view) { | |
if (isChoiceMammal) { | |
score = +1; | |
} | |
if (isChoiceMammal) { | |
score += 1; | |
} | |
if (isChoiceNile) { | |
score += 1; | |
} | |
String continentAnswer = editTextContinent.getText().toString(); | |
if (continentAnswer == "London") { | |
score += 1; | |
} | |
display("Your Score Is: " + score); | |
} | |
public void reset(View view) { | |
score = 0; | |
isChoiceMammal = false; | |
isChoiceNile = false; | |
// Reset all CheckBoxes | |
chkBxNileRiver.setChecked(false); | |
chkBxZambeziRiver.setChecked(false); | |
chkBxNepalRiver.setChecked(false); | |
// Reset all RadioButtons | |
for (int i = 0; i < radioGroupAnimal.getChildCount(); i++) { | |
RadioButton childButton = (RadioButton) radioGroupAnimal.getChildAt(i); | |
childButton.setChecked(false); | |
} | |
display("Your Score Is:" + score); | |
} | |
} | |
hi, pls can i see code for your xml?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you Master!