【问题标题】:Trouble with the radio buttons in my quiz app我的测验应用程序中的单选按钮有问题
【发布时间】:2023-05-09 21:14:01
【问题描述】:

我正在开发一个测验应用程序。 应用程序中有 5 个问题。 每个问题有 4 个选项。 对于每个问题,我都在使用广播组。 到目前为止,当检查正确的答案单选按钮时,我只能显示每个正确答案的吐司消息。 单击提交按钮后,我想在 Toast 消息中显示测验结果。 我已经提到了我想要显示的 java 代码中的要点。它们在 submitQuiz 方法中。 下面,我提供我的应用程序代码。

XML 代码:

<ScrollView 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="match_parent"
    tools:context="com.infinitystone.mani.quiz.MainActivity">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:gravity="center_horizontal"
            android:text="General Knowledge Quiz"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="italic" />

        <EditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:hint="Enter Your Name"
            android:inputType="textCapWords" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 1:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_1"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1a" />

            <RadioButton
                android:id="@+id/radio_answer_1b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1b" />

            <RadioButton
                android:id="@+id/radio_answer_1c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1c" />

            <RadioButton
                android:id="@+id/radio_answer_1d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 2:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_2"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_2a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2a" />

            <RadioButton
                android:id="@+id/radio_answer_2b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2b" />

            <RadioButton
                android:id="@+id/radio_answer_2c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2c" />

            <RadioButton
                android:id="@+id/radio_answer_2d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 3:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_3"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_3a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3a" />

            <RadioButton
                android:id="@+id/radio_answer_3b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3b" />

            <RadioButton
                android:id="@+id/radio_answer_3c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3c" />

            <RadioButton
                android:id="@+id/radio_answer_3d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 4:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_4"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_4a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4a" />

            <RadioButton
                android:id="@+id/radio_answer_4b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4b" />

            <RadioButton
                android:id="@+id/radio_answer_4c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4c" />

            <RadioButton
                android:id="@+id/radio_answer_4d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 5:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_5"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_5a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5a" />

            <RadioButton
                android:id="@+id/radio_answer_5b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5b" />

            <RadioButton
                android:id="@+id/radio_answer_5c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5c" />

            <RadioButton
                android:id="@+id/radio_answer_5d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:onClick="submitQuiz"
                android:text="Submit"
                android:textAllCaps="true" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="16dp"
                android:layout_weight="1"
                android:onClick="resetQuiz"
                android:text="Reset"
                android:textAllCaps="true" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

Java 代码:

package com.infinitystone.mani.quiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    // This method is called when the radio button with correct answer is checked
    public void radioGroup(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        switch (view.getId()) {

            case R.id.radio_answer_1b:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_2a:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_3b:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_4a:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_5d:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    // This method is called when the submit button is clicked
    public void submitQuiz(View view){
      /*
      * I want to display the following details when the submit button is clicked
      *
      * Number of questions attempted
      * Number of right answers
      * Number of wrong answers
      *
      * All this summary in a toast message
      * */
    }

}

String.xml 文件:

<resources>
    <string name="app_name">Quiz</string>

    <string name="question_1">Hitler party which came into power in 1933 is known as ?</string>
    <string name="answer_1a">Labour Party</string>
    <string name="answer_1b">Nazi Party</string>
    <string name="answer_1c">Ku-Klux-Klan</string>
    <string name="answer_1d">Democratic Party</string>
    <!-- Correct answer: Nazi party -->

    <string name="question_2">First human heart transplant operation conducted by Dr. Christiaan Barnard on Louis Washkansky, was conducted in which year ?</string>
    <string name="answer_2a">1967</string>
    <string name="answer_2b">1968</string>
    <string name="answer_2c">1958</string>
    <string name="answer_2d">1922</string>
    <!-- Correct answer: 1967 -->

    <string name="question_3">In which year Georgia, Uzbekistan and Turkmenistan became the members of UN ?</string>
    <string name="answer_3a">1991</string>
    <string name="answer_3b">1992</string>
    <string name="answer_3c">1993</string>
    <string name="answer_3d">1994</string>
    <!-- Correct answer: 1992 -->

    <string name="question_4">Escape velocity of a rocket fired from the earth towards the moon is a velocity to get rid of what ?</string>
    <string name="answer_4a">Earth gravitational pull</string>
    <string name="answer_4b">Moon gravitational pull</string>
    <string name="answer_4c">Centripetal force due to the earth rotation</string>
    <string name="answer_4d">Pressure of the atmosphere</string>
    <!-- Correct answer: Earth gravitational pull  -->

    <string name="question_5">Which instrument is used for seeing objects at the surface of water from a submarine under water ?</string>
    <string name="answer_5a">Kaleidoscope</string>
    <string name="answer_5b">Spectroscope</string>
    <string name="answer_5c">Telescope</string>
    <string name="answer_5d">Periscope</string>
    <!-- Correct answer: Periscope  -->
</resources>

【问题讨论】:

    标签: android radio-group


    【解决方案1】:

    既然您说您可以在检查按钮时显示 Toast 消息,我假设您知道如何在代码中定义和查找视图。

    您现在需要做的是声明一个 Button 变量并为其查找视图。您可以在 onCreate() 方法中执行此操作:

    btn_submit = (Button) findViewByID(R.id.btn_submit)
    

    其中 btn_submit 是您应该在活动中定义的全局变量,因为您需要在其他方法中访问它。顺便说一句,你没有在你的 R.layout.activity_main.xml 中为按钮指定 id,你应该这样做(我假设它是 btn_sumbit,你应该根据你的命名更改查找视图代码)。

    现在你需要做的是监听按钮点击动作。因此,在找到 Button 的视图后,您需要给它一个 onClickListener。您可以通过以下方式执行此操作:

    btn_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                      submitQuiz();
                }
    });
    

    现在按钮将监听点击事件,并在点击时调用您的 submitQuiz() 方法。

    此外,您应该声明全局变量并为每个 RadioGroup 查找视图。这样您就可以在 submitQuiz() 方法中使用它们。现在您可以使用以下代码进行评分逻辑:

    switch ( rg_question1.getCheckedRadioButtonId() ) {
                            case R.id.answer_1a:
                                // wrong
                                break;
                            case R.id.rb_answer_1b:
                                // correct
                                correctCount++;
                                correctQustionList.add("qustion1");
                                break;
                            case ...
    }
    

    最后,显示 toast 消息。我相信你知道如何做这部分。

    【讨论】:

    • 我必须为提交按钮和每个单选组分配一个 id 吗?
    • 是的。否则你不会知道点击了哪个按钮
    • 我没有正确理解你的代码..你能给我一些更详细的代码
    • 您必须为要在代码中引用的每个视图分配 ID。在您的情况下,Buttons 和 RadioGroups 和 RadioButtons 必须具有 ID,否则您的设备将如何知道您要引用哪个视图?我使用了我喜欢的视图命名风格,这可能会让你感到困惑,我很抱歉。上面的代码中,btn_xxx代表Button,rg_xxx代表RadioGroup,rb_xxx代表RadioButton等。所以btn_submit是一个变量,我想引用布局文件中的提交Button。
    • 基本上需要1)声明View变量,如Button、RadioGroup。 2) 查找这些变量的引用,即 btn_submit = (Button) findViewByID(R.id.btn_submit) 3) 为按钮注册监听器和回调。即 btn_submit.setOnClickListener(...)。就是这样。
    最近更新 更多