【问题标题】:How to find out which radiobutton has been selected by the user for comparing with the correct one in QuestionApp in Android如何找出用户选择了哪个单选按钮以与Android的QuestionApp中的正确单选按钮进行比较
【发布时间】:2021-03-12 18:45:54
【问题描述】:

这里有问题,

我想找出用户选择的 RadioButton 并将其与正确的 RadioButton 进行比较。 我有 3 个问题,它的答案在一个 ArrayList 中。 当我做一个日志来找出哪个被选中时,这是输出:

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

                    }
                });

            }

【问题讨论】:

    标签: java android android-studio radio-button


    【解决方案1】:

    为了使其尽可能简单,这里有一个您可以信赖的解决方案:

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

    在MainActivity里面的onCreate:

    // 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 " );
            }
    
                    }
                });
    

    【讨论】:

    • 谢谢,但你知道这不仅仅是一个问题,例如我有 3 个问题,所以我认为这不是解决方案。
    • 你可以依赖java部分来满足你的需求
    猜你喜欢
    • 2016-06-15
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    相关资源
    最近更新 更多