简体   繁体   English

如何找出用户选择了哪个单选按钮以与 Android 中 QuestionApp 中的正确单选按钮进行比较

[英]How to find out which radiobutton has been selected by the user for comparing with the correct one in QuestionApp in Android

Theres a problem here,这里有问题,

I want to find out the selected RadioButton by user and compare it with the correct one.我想找出用户选择的 RadioButton 并将其与正确的 RadioButton 进行比较。 I have 3 question and its answer is in one ArrayList.我有 3 个问题,它的答案在一个 ArrayList 中。 When I make a log to find out which one has been selected, this is the output:当我做一个日志找出哪个被选中时,这是output:

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

The answer of the 3rd one, didn't save and the answer of the second questions will save for 3rd one.第三题的答案,没有保存,第二题的答案将保存到第三题。 please help me.请帮我。 what should I do我应该怎么办

Here is my questions code:这是我的问题代码:

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

This is how I reached the selected radiobutton by user:这就是我到达用户选择的单选按钮的方式:

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

And here is the Log and the other things:这是日志和其他内容:

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

                    }
                });

            }

To make it as simple as possible here is a solution that you can rely on:为了使其尽可能简单,这是您可以依赖的解决方案:

<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>

In the MainActivity inside onCreate:在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 " );
        }

                }
            });

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM