首页 > 解决方案 > 如果多次检查正确的单选按钮,则停止分数增加

问题描述

我有 2 个 radioGroup 问题,当检查正确答案时,结果按预期增加了 20,但是当按几次正确的单选按钮时,它不断增加分数(结果)的问题,我希望分数(结果)是即使按了正确的单选按钮几次,也只会增加一次!另外,如何重置所有问题的所有答案以重新开始测验?提前谢谢

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.nour.quiz.MainActivity">

    <ImageView
        android:id="@+id/android_pyramids_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="16dp">


        <TextView
            android:id="@+id/Q1"
            style="@style/QuestionsTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="@string/Q1" />

        <EditText
            android:id="@+id/age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hint1"
            android:inputType="number"
            style="@style/AnswersTextStyle"/>


        <TextView
            android:id="@+id/Q2"
            style="@style/QuestionsTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="@string/Q2" />

        <EditText
            android:id="@+id/pharoahName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hint2"
            android:inputType="textCapWords"
            style="@style/AnswersTextStyle"/>


        <TextView
            android:id="@+id/Q3"
            style="@style/QuestionsTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="@string/Q3" />

        <RadioGroup
            android:id="@+id/radioGroupOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/yesAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="result"
                android:text="@string/RadioButton1"
                style="@style/AnswersTextStyle"/>

            <RadioButton
                android:id="@+id/noAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="result"
                android:text="@string/RadioButton2"
                style="@style/AnswersTextStyle"/>

        </RadioGroup>


        <TextView
            android:id="@+id/Q4"
            style="@style/QuestionsTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="@string/Q4" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <CheckBox
                android:id="@+id/wonderOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:onClick="wonderOne"
                android:text="@string/CheckBox1"
                style="@style/AnswersTextStyle"/>

            <CheckBox
                android:id="@+id/wonderTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:onClick="wonderTwo"
                android:text="@string/CheckBox2"
                style="@style/AnswersTextStyle"/>

            <CheckBox
                android:id="@+id/wonderThree"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:onClick="wonderThree"
                android:text="@string/CheckBox3"
                style="@style/AnswersTextStyle"/>

        </LinearLayout>

        <TextView
            android:id="@+id/Q5"
            style="@style/QuestionsTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="@string/Q5" />


        <RadioGroup
            android:id="@+id/radioGroupTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/FranceAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="FranceButton"
                android:text="@string/RadioButton3"
                style="@style/AnswersTextStyle"/>

            <RadioButton
                android:id="@+id/EnglandAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="EnglandButton"
                android:text="@string/RadioButton4"
                style="@style/AnswersTextStyle"/>

            <RadioButton
                android:id="@+id/EgyptAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="EgyptButton"
                android:text="@string/RadioButton5"
                style="@style/AnswersTextStyle"/>

        </RadioGroup>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@drawable/button"
                android:onClick="result"
                android:text="@string/button" />

        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

 private int result = 0;
EditText age, name;
CheckBox wonderOne, wonderTwo, wonderThree;
RadioGroup radioGroup1, radioGroup2;
RadioButton yes, no, france, england, egypt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    age = findViewById(R.id.age);
    name = findViewById(R.id.pharoahName);
    radioGroup1 = findViewById(R.id.radioGroupOne);
    radioGroup2 = findViewById(R.id.radioGroupTwo);
    wonderOne = findViewById(R.id.wonderOne);
    wonderTwo = findViewById(R.id.wonderTwo);
    wonderThree = findViewById(R.id.wonderThree);
}


//Result of Q4
// Do nothing if wonder one is checked
public void wonderOne(View view) {
    if (wonderTwo.isChecked() && wonderThree.isChecked() && !wonderOne.isChecked()) {
        result += 20;

    } else {
    }

}
//Result of Q4
//Add 20 to result if wonder two is checked
public void wonderTwo(View view) {
    if (wonderTwo.isChecked() && wonderThree.isChecked() && !wonderOne.isChecked()) {
        result += 20;

    } else {
    }
}
//Add 20 to result if wonder three is checked
public void wonderThree(View view) {
    if (wonderTwo.isChecked() && wonderThree.isChecked() && !wonderOne.isChecked()) {
        result += 20;

    } else {
    }

}
//Result of Q5
public void FranceButton(View view) {
    int radioId = radioGroup2.getCheckedRadioButtonId();
    france = findViewById(radioId);

}
//Result of Q5
public void EnglandButton(View view) {
    int radioId = radioGroup2.getCheckedRadioButtonId();
    england = findViewById(radioId);

}
//Result of Q5
public void EgyptButton(View view) {
    int radioId = radioGroup2.getCheckedRadioButtonId();
    egypt = findViewById(radioId);

    //Add 20 to result if Egypt is checked
    if (egypt.isChecked()) {
        result += 20;
    } else {
    }
}
//Result of Q3


//This method is called when submit answers button is clicked
public void result(View view) {

    //Result of Q1
    // This method is to prevent app from crashing if age is not entered
    try {
        //Range of accepted age is between 21 & 50 years old
        int range = Integer.parseInt(age.getText().toString());

        //Add 20 to result if age is between 21 & 50
        if (range >= 21 && range <= 50) {
            result += 20;

        } else {

        }
    } catch (Exception ex) {
        System.out.println("you must enter your age");

    }

    //Result of Q2
    // Add 20 to result if Name = Cleopatra
    if (name.getText().toString().equals("Cleopatra")) {
        result += 20;

    } else {

    }

    //Result of Q3
    yes = findViewById(R.id.yesAnswer);
    no = findViewById(R.id.noAnswer);

    if (yes.isChecked() && !no.isChecked()){
        result += 20;
    }


    //Calculate final result
    if (result >= 60) {
        ImageView imageView = findViewById(R.id.android_pyramids_image_view);
        imageView.setImageResource(R.drawable.pyramids_hdpi);


        Toast.makeText(getApplicationContext(), "Your result is: " + result + "/100" +
                ", Congratulations! YOU won a trip to Egypt.", Toast.LENGTH_LONG).show();

    } else {
        Toast.makeText(getApplicationContext(), "Your result is: " + result + "/100" +
                ", Hard luck this time!", Toast.LENGTH_LONG).show();
    }
}


}

标签: javaandroidradio-button

解决方案


(最终正确答案)

公共类 MainActivity 扩展 AppCompatActivity {

private int result = 0;
EditText age, name;
CheckBox wonderOne, wonderTwo, wonderThree;
RadioGroup radioGroup1, radioGroup2;
RadioButton yes, no, france, england, egypt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    age = findViewById(R.id.age);
    name = findViewById(R.id.pharoahName);
    radioGroup1 = findViewById(R.id.radioGroupOne);
    radioGroup2 = findViewById(R.id.radioGroupTwo);
    wonderOne = findViewById(R.id.wonderOne);
    wonderTwo = findViewById(R.id.wonderTwo);
    wonderThree = findViewById(R.id.wonderThree);
}

//Result of Q3
public void yesButton(View view) {
    int radioId = radioGroup1.getCheckedRadioButtonId();
    yes = findViewById(radioId);
    //Add 20 to result if yes is checked
    if (yes.isChecked()) {
        result += 20;
        //disabling the radioButtons after the user has selected an option in it
        //This is to prevent rechecking of checked radioButton and stop score (result) increment
        for (int i = 0; i < radioGroup1.getChildCount(); i++) {
            radioGroup1.getChildAt(i).setEnabled(false);
        }

    }else{

    }
}
//Result of Q3
public void noButton(View view) {
    int radioId2 = radioGroup1.getCheckedRadioButtonId();
    no = findViewById(radioId2);
    if (no.isChecked()){
        //disabling the radioButtons after the user has selected an option in it
        //This is to prevent rechecking of checked radioButton and stop score (result) increment
        for (int i = 0; i < radioGroup1.getChildCount(); i++) {
            radioGroup1.getChildAt(i).setEnabled(false);
        }

    }else {

    }
}

//Result of Q4
// Do nothing if wonder one is checked
public void wonderOne(View view) {
    if (wonderTwo.isChecked() && wonderThree.isChecked() && !wonderOne.isChecked()) {
        result += 20;

    } else {
    }

}
//Result of Q4
//Add 20 to result if wonder two is checked
public void wonderTwo(View view) {
    if (wonderTwo.isChecked() && wonderThree.isChecked() && !wonderOne.isChecked()) {
        result += 20;

    } else {
    }
}

//Result of Q4
//Add 20 to result if wonder three is checked
public void wonderThree(View view) {
    if (wonderTwo.isChecked() && wonderThree.isChecked() && !wonderOne.isChecked()) {
        result += 20;

    } else {
    }

}
//Result of Q5
public void FranceButton(View view) {
    int radioId = radioGroup2.getCheckedRadioButtonId();
    france = findViewById(radioId);
    if (france.isChecked()){
        //disabling the radioButtons after the user has selected an option in it
        //This is to prevent rechecking of checked radioButton and stop score (result) increment
        for (int x = 0; x < radioGroup2.getChildCount(); x++) {
            radioGroup2.getChildAt(x).setEnabled(false);
        }
    }else {

    }
}

//Result of Q5
public void EnglandButton(View view) {
    int radioId = radioGroup2.getCheckedRadioButtonId();
    england = findViewById(radioId);
    if (england.isChecked()){
        //disabling the radioButtons after the user has selected an option in it
        //This is to prevent rechecking of checked radioButton and stop score (result) increment
        for (int x = 0; x < radioGroup2.getChildCount(); x++) {
            radioGroup2.getChildAt(x).setEnabled(false);
        }
    }else {

    }

}
//Result of Q5
public void EgyptButton(View view) {
    int radioId = radioGroup2.getCheckedRadioButtonId();
    egypt = findViewById(radioId);

    //Add 20 to result if Egypt is checked
    if (egypt.isChecked()) {
        result += 20;
        //disabling the radioButtons after the user has selected an option in it
        //This is to prevent rechecking of checked radioButton and stop score (result) increment
        for (int x = 0; x < radioGroup2.getChildCount(); x++) {
            radioGroup2.getChildAt(x).setEnabled(false);
        }
    } else {
    }
}
//Result of Q3


//This method is called when submit answers button is clicked
public void result(View view) {

    //Result of Q1
    // This method is to prevent app from crashing if age is not entered
    try {
        //Range of accepted age is between 21 & 50 years old
        int range = Integer.parseInt(age.getText().toString());

        //Add 20 to result if age is between 21 & 50
        if (range >= 21 && range <= 50) {
            result += 20;

        } else {

        }
    } catch (Exception ex) {
        System.out.println("you must enter your age");

    }

    //Result of Q2
    // Add 20 to result if Name = Cleopatra
    if (name.getText().toString().equals("Cleopatra")) {
        result += 20;

    } else {

    }

    //Calculate final result
    if (result >= 60) {
        //Display pyramid image
        ImageView imageView = findViewById(R.id.android_pyramids_image_view);
        imageView.setImageResource(R.drawable.pyramids_hdpi);
        imageView.setVisibility(View.VISIBLE);


        //Display Toast message
        Toast.makeText(getApplicationContext(), "Your result is: " + result + "/100" +
                ", Congratulations! YOU won a trip to Egypt.", Toast.LENGTH_LONG).show();

        //Clear all answers and reset result to zero to restart quiz
        name.getText().clear();
        age.getText().clear();
        wonderOne.setChecked(false);
        wonderTwo.setChecked(false);
        wonderThree.setChecked(false);
        radioGroup1.clearCheck();
        //Enabling the radioButtons again for each radioGroup
        for (int i = 0; i < radioGroup1.getChildCount(); i++) {
            radioGroup1.getChildAt(i).setEnabled(true);
        }
        radioGroup2.clearCheck();
        //Enabling the radioButtons again for each radioGroup
        for (int x = 0; x < radioGroup2.getChildCount(); x++) {
            radioGroup2.getChildAt(x).setEnabled(true);
        }
        result = 0;
    } else {
        //Remove pyramid image
        ImageView imageView = findViewById(R.id.android_pyramids_image_view);
        imageView.setImageResource(R.drawable.pyramids_hdpi);
        //This code to make pyramid image visible again after removal by wrong answers
        imageView.setVisibility(View.INVISIBLE);

        //Display Toast message
        Toast.makeText(getApplicationContext(), "Your result is: " + result + "/100" +
                ", Hard luck this time!", Toast.LENGTH_LONG).show();

        //Clear all answers and reset result to zero to restart quiz
        name.getText().clear();
        age.getText().clear();
        wonderOne.setChecked(false);
        wonderTwo.setChecked(false);
        wonderThree.setChecked(false);
        radioGroup1.clearCheck();
        //Enabling the radioButtons again for each radioGroup
        for (int i = 0; i < radioGroup1.getChildCount(); i++) {
            radioGroup1.getChildAt(i).setEnabled(true);
        }
        radioGroup2.clearCheck();
        //Enabling the radioButtons again for each radioGroup
        for (int x = 0; x < radioGroup2.getChildCount(); x++) {
            radioGroup2.getChildAt(x).setEnabled(true);
        }

        result = 0;

    }
}

}


推荐阅读