如何让无线电组更改测验的onclick? (使用Arraylist)

时间:2019-04-24 10:51:59

标签: android

我正在尝试使用单选按钮在我的应用程序中进行测验。 我已经创建了一个Arraylist,并且试图找出如何将我的问题/答案分配给例如“ Q1”的问题,并让其更改问的问题和单选按钮上的选项。

在设置Arraylist之前,我尝试过一些可以随机化并返回数字的方法,但是我觉得最好使用Arraylist来尝试

Java代码:

public class GrammarTest extends AppCompatActivity implements View.OnClickListener {
    private RadioGroup radioSexGroup;
    private RadioButton radioSexButton;
    private Button btnDisplay;
    private Button btnNext;
    private int score = 0;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grammar_test);



        addListenerOnButton();
        btnNext.setVisibility(View.GONE);

        btnNext = (Button) findViewById(R.id.button2);
        btnNext.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                btnNext.setVisibility(View.GONE);
            }


        });



    }

    public void addListenerOnButton() {

        radioSexGroup = (RadioGroup) findViewById(R.id.radioGroup);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);



        btnDisplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // get selected radio button from radioGroup
                int selectedId = radioSexGroup.getCheckedRadioButtonId();


                radioSexButton = (RadioButton) findViewById(selectedId);



                if(radioSexButton == (RadioButton) findViewById(R.id.radio1)){
                    giveReply("Wrong answer");

                } else {
                    giveReply("You are right!");
                }




            }

        });













    }

    public void giveReply(String text)
    {
        Toast.makeText(GrammarTest.this,
                text, Toast.LENGTH_SHORT).show();

        btnNext.setVisibility(View.VISIBLE);
    }
   public static void main(String[]args) {
       ArrayList questionList = new ArrayList();
       questionList.add("Q1");
       questionList.add("Q2");
       questionList.add("Q3");
       questionList.add("Q4");
       questionList.add("Q5");
       questionList.add("Q6");
       questionList.add("Q7");
       questionList.add("Q8");
       questionList.add("Q9");
       questionList.add("Q10");
       questionList.add("Q11");
       questionList.add("Q12");
       questionList.add("Q13");
       questionList.add("Q14");
       questionList.add("Q15");
       questionList.add("Q16");
       questionList.add("Q17");
       questionList.add("Q18");
       questionList.add("Q19");
       questionList.add("Q20");
       Collections.shuffle(questionList);
    }





    @Override
    public void onClick(View v) {

    }
}


XML代码:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="728dp"
        android:orientation="vertical"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="3dp"
        android:background="@color/blue">


        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="185dp"
            android:layout_marginEnd="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginBottom="482dp">

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/radioGroup"
                android:layout_alignTop="@+id/radioGroup"
                android:checked="true"
                android:text="@string/radio_male" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/radioGroup"
                android:layout_alignTop="@+id/radioGroup"
                android:text="@string/radio_female" />


        </RadioGroup>

        <Button
            android:id="@+id/btnDisplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="168dp"
            android:layout_marginLeft="168dp"
            android:layout_marginEnd="155dp"
            android:layout_marginRight="155dp"
            android:layout_marginBottom="315dp"
            android:text="@string/btn_display" />


        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="69dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="82dp"
            android:layout_marginRight="82dp"
            android:layout_marginBottom="622dp"
            android:text="@string/large_text41" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="301dp"
            android:layout_marginLeft="301dp"
            android:layout_marginEnd="22dp"
            android:layout_marginRight="22dp"
            android:layout_marginBottom="317dp"
            android:text="Button" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

我希望建立一个简单的测验,随机询问您问题并记住您已经回答了哪些问题。到目前为止,我只在它问一个问题时才起作用

1 个答案:

答案 0 :(得分:0)

您可以使用ViewPager逐一显示问题答案 对于答案,您可以根据获得的答案数量动态添加单选按钮。

当用户选择答案时,您可以在某些HashMap类型数组列表中添加问题ID answer-id

 **Question Fragment code**
  

提出问题并动态添加单选按钮

 txtQuestions.setText(question);

    for (int i = 0; i < answerModelArrayList.size(); i++) {

        RadioButton radioButton = (RadioButton) LayoutInflater.from(getContext()).inflate(R.layout.layout_answer, null);

        radioButton.setId(View.generateViewId());

        radioButton.setText(answerModelArrayList.get(i).getAnswer());

        questionsBinding.rgAnswer.addView(radioButton);

        if (QuestionPagerFragment.answeredQuesMap.get(questionId) != null && QuestionPagerFragment.answeredQuesMap.get(questionId) == i) {

            radioButton.setChecked(true);

        }
    }
  

用于答案单选按钮

questionsBinding.rgAnswer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            for (int i = 0; i < answerModelArrayList.size(); i++) {

                questionsBinding.ivNext.setAlpha(1f);

                answerId = answerModelArrayList.get(i).getAnswerId();

                if (answerModelArrayList.get(i).getAnswer().equals(((RadioButton) group.findViewById(group.getCheckedRadioButtonId())).getText().toString())) {

                    QuestionPagerFragment.answeredQuesMap.put(questionId, answerModelArrayList.get(i).getPoint());

                    allowNext = true;

                    break;

                }

            }

        }
    });

如果您还有其他问题要问我,那么这段代码对我来说是完全有效的。