java—如何找出用户选择了哪个单选按钮,以便与android中questionapp中的正确单选按钮进行比较

t1qtbnec  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(387)

这里有个问题,
我想找出用户选择的单选按钮,并与正确的比较。我有3个问题,答案在一个数组列表中。当我做一个日志来找出哪个被选中时,这是输出:

2020-11-30 23:12:19.128 5696-5696/com.example.test1 E/Question: Where is Iran ?  Asia
2020-11-30 23:12:19.128 5696-5696/com.example.test1 E/Question: Where is Sweden ?  Europe
2020-11-30 23:12:19.128 5696-5696/com.example.test1 E/Question: Where is Mexico ?  Europe

第三个问题的答案未保存,第二个问题的答案将保存到第三个问题。请帮帮我。我该怎么办
这是我的问题代码:

QuestionModel q1 = new QuestionModel();
    q1.setText("Where is Iran ?");
    q1.setOp1("Asia");
    q1.setOp2("Europe");
    q1.setOp3("America");
    q1.setOp4("Africa");
    q1.setTrue_ans("Asia");
    q1.setScore(5);
    questions.add(q1);

    QuestionModel q2 = new QuestionModel();
    q2.setText("Where is Sweden ?");
    q2.setOp1("America");
    q2.setOp2("Africa");
    q2.setOp3("Asia");
    q2.setOp4("Europe");
    q2.setTrue_ans("Europe");
    q2.setScore(3);
    questions.add(q2);

    QuestionModel q3= new QuestionModel();
    q3.setText("Where is Mexico ?");
    q3.setOp1("Africa");
    q3.setOp2("America");
    q3.setOp3("Europe");
    q3.setOp4("Asia");
    q3.setTrue_ans("America");
    q3.setScore(4);
    questions.add(q3);

这是我如何通过用户访问所选单选按钮的:

if (radioGp.getCheckedRadioButtonId() != -1) {
                QuestionModel model = questions.get(con);
                int id = radioGp.getCheckedRadioButtonId();
                RadioButton useranswer = findViewById(id);
                model.setUser_ans(useranswer.getText().toString());
            }

这是日志和其他东西:

if (con == 2) {
                nextbtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (QuestionModel q : questions) {
                            Log.e("Question ", q.getText() + "  " + q.getUser_ans());
                            String user_ans = q.getUser_ans();
                            String true_ans = q.getTrue_ans();

                            if( true_ans.equals(user_ans) ) {
                                score += q.getScore();
                            }

                        }

                        scorebox.setText(Integer.toString(score));

                    }
                });

            }
iq0todco

iq0todco1#

为了尽可能简单,您可以依赖以下解决方案:

<RelativeLayout
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/question_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"
            android:text="Where is Iran ?"
            android:textSize="20sp" />

        <RadioGroup
            android:layout_below="@+id/question_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:id="@+id/radioGroup">

            <RadioButton
                android:id="@+id/radio_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Asia"/>

            <RadioButton
                android:id="@+id/radio_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Europe"/>

            <RadioButton
                android:id="@+id/radio_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="America"/>

            <RadioButton
                android:id="@+id/radio_four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Africa"/>

        </RadioGroup>

        <TextView
            android:id="@+id/text_view_selected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioGroup"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="27dp"
            android:text="Your answer is: "
            android:textSize="20sp" />

        <Button
            android:id="@+id/button_apply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_view_selected"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="37dp"
            android:text="apply" />

    </RelativeLayout>

在oncreate内的mainactivity中:

// the RadioGroup holding the radio buttons
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
// the text that will display the result
        textView = (TextView) findViewById(R.id.text_view_selected);
// the button that will check if the selected answer is correct or wrong
        Button buttonApply = (Button) findViewById(R.id.button_apply);

        buttonApply.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

// This gets the id of the selected radio button BY THE USER
        int radioId = radioGroup.getCheckedRadioButtonId();
        RadioButton selectedAnswer = (RadioButton) findViewById(radioId);

        // THIS IS YOUR CORRECT ANSWER
        // if the correct answer is Africa then R.id.radio_four etc
        RadioButton correctAnswerButton = (RadioButton) findViewById(R.id.radio_one);

        // this gets the text of your correct answer
        CharSequence correctAnswer = correctAnswerButton.getText();

        // NOW compare the selected answer to the correct answer
        if (selectedAnswer.getText() == correctAnswer) {
            textView.setText("Your answer is right");
        } else {
            textView.setText( "Your answer is wrong " );
        }

                }
            });


相关问题