如果多次检查正确的radiobutton,则停止分数增加

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果多次检查正确的radiobutton,则停止分数增加相关的知识,希望对你有一定的参考价值。

我有2个radioGroup问题,当检查正确的答案时,结果按预期增加了20,但问题是当按正确的radiobutton几次时,它继续增加得分(结果),我想得分(结果)是即使正确的无线电按钮被按下几次也只增加一次!另外,如何重置所有问题的所有答案以重新开始测验?,提前感谢

<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();
    }
}


}
答案

有多种解决方案。其中之一是如果选择了答案,则禁用无线电组按钮,因此用户将无法更改其选项或再次选择相同的选项。

这可以通过在用户选择其中的选项后禁用每个radioGroup的radioButtons来完成

for (int i = 0; i < radioGroup.getChildCount(); i++) {
       radioGroup.getChildAt(i).setEnabled(false);
}

关于问题的第二部分,只需使用以下方法之一清除测验所需的每个元素:

radioButton.setChecked(false);
checkBox.setChecked(false);
editText.getText().clear();

如果您不想禁用已经回答的测验问题,您可以为测验的每个部分设置一个布尔变量(例如private boolean answeredQuizSection1 = false;),初始值为false,如果用户选择该部分的任何答案,则更改相应的变量到true。您只需在if语句的条件中添加此变量,以增加结果点。

另一答案

(最终正确答案)

公共类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;

    }
}

}

以上是关于如果多次检查正确的radiobutton,则停止分数增加的主要内容,如果未能解决你的问题,请参考以下文章

7-2 符号配对 (20 分)

检查输入字段中的空值的正确方法是啥[重复]

如何检查条件,如果条件为真,则停止报价器并退出应用程序,如果条件不成立并且发生超时,则退出应用程序

7-18 二分法求多项式单根(20 分)

如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回布尔值 true 停止使用 Javascript 进行检查

检查操作系统版本,如果版本正确则发出命令