I'm developing a quiz. Using RadioButton as Answer. So far I created using the onClick method on each RadioButton, but it is not effective.
So I would like to call each question ask1();
, ask2();
within the rate(View view)
method and from there calculate the total score of the user responses showing a Toast.
But when calling for example the ask6();
method within the rate(View view);
of error. Even putting the view parameter ask(view);
by what rate it has is a view.
public class MainActivity extends AppCompatActivity {
double rating = 0;
String userAnswer;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ask1( View view ) {
//See if checked
boolean checked = ((RadioButton) view).isChecked();
//Do some if checked
switch (view.getId()) {
case R.id.answer1_ask1:
if (checked) {
rating += 1;
break;
}
default:
if (checked) {
rating -= 1;
break;
}
}
}
public void ask2( View view ) {
//is checked?
boolean checked = ((CheckBox) view).isChecked();
//Do some
switch (view.getId()) {
case R.id.answer1_ask2:
if (checked) rating -= 1;
case R.id.answer2_ask2:
if (checked) rating += 0.5;
case R.id.answer3_ask2:
if (checked) rating += 0.5;
}
}
public void ask3(){
EditText answer = findViewById(R.id.answer1_ask3);
userAnswer = String.valueOf(answer.getText());
if (userAnswer.equals("Láctea")) {
rating += 1;
} else if (userAnswer.equals("Via Láctea")) {
rating += 1;
} else {
rating -= 1;
}
}
public void ask4(View view){
//See if checked
boolean checked = ((RadioButton) view).isChecked();
//Do!
switch (view.getId()) {
case R.id.answer1_ask4:
if (checked) {
rating += 1;
break;
}
case R.id.answer2_ask4:
if (checked) {
rating -= 1;
break;
}
case R.id.answer3_ask4:
if (checked) {
rating -= 1;
break;
}
}
}
public void ask5(View view){
//is checked?
boolean checked = ((RadioButton)view).isChecked();
//Do it now!!!!
switch (view.getId()){
case R.id.answer1_ask5:
if(checked){
rating += 1;
}
case R.id.answer2_ask5:
if (checked) {
rating -= 1;
}
}
}
public void ask6(View view){
//is checked?
boolean checked = ((RadioButton)view).isChecked();
//Please do fast!
switch (view.getId()){
case R.id.answer1_ask6:
if(checked){
rating += 1;
}
case R.id.answer2_ask6:
if(checked){
rating -= 1;
}
}
}
public void rate( View view ) {
// ask6(View view); Why can't I call?
// ask6(view); Why bug?
ask6();
Toast.makeText(this, ("Oi "+ rating ), Toast.LENGTH_SHORT).show();
rating = 0;
}
/*
public void ask3( View view ) {
//was wrote?
//String answer = ((EditText)view).getText(findViewById(R.id.answer1_ask3)).toString();
//was wrote?
EditText answer = findViewById(R.id.answer1_ask3);
String userAnswer = String.valueOf(answer.getText());
String rightAnswer1 = "Via Láctea";
String rightAnswer2 = "Láctea";
//do some
if(userAnswer == rightAnswer1) rating += 1;
if (userAnswer == rightAnswer2) rating += 1;
else rating -= 1;
// if (userAnswer == "a"){
}
*/
}
Part of XML:
<RadioGroup
android:layout_marginLeft="72dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<RadioButton
android:id="@+id/answer1_ask6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer1_ask6"/>
<RadioButton
android:id="@+id/answer2_ask6"
android:layout_marginVertical="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer2_ask6"/>
</RadioGroup>
<Button
android:text="RESPONDER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rate"
android:layout_marginLeft="72dp"
android:layout_marginBottom="20dp"/>
</LinearLayout>
=================== Adding an edit =============
In the Android documentation, there is an example of how to use RadioButton:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
The good thing about this method is that I just have to play the id of each RadioButton and use the switch to execute everything at once. So I edited my code to make each RadioButton case I have on the quiz. It looks like this:
public void askRadioButton(View view){
//is checked?
boolean checked = ((RadioButton)view).isChecked();
//Do it now!!!!
switch (view.getId()){
case R.id.answer1_ask1:
if (checked) {
rating += 1;
break;
}
case R.id.answer1_ask2:
if (checked) rating -= 1;
break;
case R.id.answer2_ask2:
if (checked) rating += 0.5;
break;
case R.id.answer3_ask2:
if (checked) rating += 0.5;
break;
case R.id.answer1_ask4:
if (checked) {
rating += 1;
break;
}
case R.id.answer2_ask4:
if (checked) {
rating -= 1;
break;
}
case R.id.answer3_ask4:
if (checked) {
rating -= 1;
break;
}
case R.id.answer1_ask5:
if(checked){
rating += 1;
}
case R.id.answer2_ask5:
if (checked) {
rating -= 1;
}
}
}
public void rate( View view ) {
askRadioButton();
Toast.makeText(this, ("Oi "+ rating ), Toast.LENGTH_SHORT).show();
rating = 0;
}
But I still can not properly call the new method. If I remove the view parameter, the rest of the code does not work.