Android Studio 中单选按钮的 onClick() 的替代选项

Alternate option for onClick() for Radio Button in Android Studio

我一直在尝试使用 RadioGroups 制作一个测验应用程序,每个问题都有 4 个选项。我已经让应用程序通过为所选的正确和错误答案维护一个指针来计算用户的分数。例如,在我的第一个 activity 中,有 8 个问题,每个问题都有其 RadioGroups 作为答案。如果用户选择了正确的选项,则正确答案的指针会增加,错误答案也会发生同样的情况。最后,计算出那个activity的最终得分,并用于下一个activity。

整个 MainActivity 的代码:

package com.example.myapplicationthreesubjectquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public int correctAnsBio=0, wrongAnsBio=0;
    public RadioGroup rgbq1, rgbq2, rgbq3, rgbq4, rgbq5, rgbq6, rgbq7, rgbq8;
    public RadioButton rbbq1, rbbq2, rbbq3, rbbq4, rbbq5, rbbq6, rbbq7, rbbq8;
    public int corbq1, corbq2, corbq3, corbq4, corbq5, corbq6, corbq7, corbq8;

    Button moveToChemistry;

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

        rgbq1 = findViewById(R.id.rgbq1);
        rgbq2 = findViewById(R.id.rgbq2);
        rgbq3 = findViewById(R.id.rgbq3);
        rgbq4 = findViewById(R.id.rgbq4);
        rgbq5 = findViewById(R.id.rgbq5);
        rgbq6 = findViewById(R.id.rgbq6);
        rgbq7 = findViewById(R.id.rgbq7);
        rgbq8 = findViewById(R.id.rgbq8);

        moveToChemistry = findViewById(R.id.moveToChemistry);

        corbq1 = 1;
        corbq2 = 1;
        corbq3 = 1;
        corbq4 = 1;
        corbq5 = 1;
        corbq6 = 1;
        corbq7 = 1;
        corbq8 = 1;



        moveToChemistry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int scoreInBio = (4*correctAnsBio)+((-1)*wrongAnsBio);
                Intent intent = new Intent(MainActivity.this, com.example.myapplicationthreesubjectquiz.chemistry.class);
                intent.putExtra("bioScore", scoreInBio);
                startActivity(intent);

            }
        });


    }

    public void clickedBq1(View v){

        int selected = -1;

        switch(rgbq1.getCheckedRadioButtonId())
        {
            case R.id.rbbq1o1:
                selected = 1;
                break;
            case R.id.rbbq1o2:
                selected = 2;
                break;
            case R.id.rbbq1o3:
                selected = 3;
                break;
            case R.id.rbbq1o4:
                selected = 4;
                break;
        }

        if(selected == corbq1)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq1)
        {
            wrongAnsBio++;
        }


    }
    public void clickedBq2(View v){

        int selected = -1;

        switch(rgbq2.getCheckedRadioButtonId())
        {
            case R.id.rbbq2o1:
                selected = 1;
                break;
            case R.id.rbbq2o2:
                selected = 2;
                break;
            case R.id.rbbq2o3:
                selected = 3;
                break;
            case R.id.rbbq2o4:
                selected = 4;
                break;
        }

        if(selected == corbq2)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq2)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq3(View v){

        int selected = -1;

        switch(rgbq3.getCheckedRadioButtonId())
        {
            case R.id.rbbq3o1:
                selected = 1;
                break;
            case R.id.rbbq3o2:
                selected = 2;
                break;
            case R.id.rbbq3o3:
                selected = 3;
                break;
            case R.id.rbbq2o4:
                selected = 4;
                break;
        }

        if(selected == corbq3)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq3)
        {
            wrongAnsBio++;
        }

    }

    public void clickedBq4(View v){

        int selected = -1;

        switch(rgbq4.getCheckedRadioButtonId())
        {
            case R.id.rbbq4o1:
                selected = 1;
                break;
            case R.id.rbbq4o2:
                selected = 2;
                break;
            case R.id.rbbq4o3:
                selected = 3;
                break;
            case R.id.rbbq4o4:
                selected = 4;
                break;
        }

        if(selected == corbq4)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq4)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq5(View v){

        int selected = -1;

        switch(rgbq5.getCheckedRadioButtonId())
        {
            case R.id.rbbq5o1:
                selected = 1;
                break;
            case R.id.rbbq5o2:
                selected = 2;
                break;
            case R.id.rbbq5o3:
                selected = 3;
                break;
            case R.id.rbbq5o4:
                selected = 4;
                break;
        }

        if(selected == corbq5)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq5)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq6(View v){

        int selected = -1;

        switch(rgbq6.getCheckedRadioButtonId())
        {
            case R.id.rbbq6o1:
                selected = 1;
                break;
            case R.id.rbbq6o2:
                selected = 2;
                break;
            case R.id.rbbq6o3:
                selected = 3;
                break;
            case R.id.rbbq6o4:
                selected = 4;
                break;
        }

        if(selected == corbq6)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq6)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq7(View v){

        int selected = -1;

        switch(rgbq7.getCheckedRadioButtonId())
        {
            case R.id.rbbq7o1:
                selected = 1;
                break;
            case R.id.rbbq7o2:
                selected = 2;
                break;
            case R.id.rbbq7o3:
                selected = 3;
                break;
            case R.id.rbbq7o4:
                selected = 4;
                break;
        }

        if(selected == corbq7)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq7)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq8(View v){

        int selected = -1;

        switch(rgbq8.getCheckedRadioButtonId())
        {
            case R.id.rbbq8o1:
                selected = 1;
                break;
            case R.id.rbbq8o2:
                selected = 2;
                break;
            case R.id.rbbq8o3:
                selected = 3;
                break;
            case R.id.rbbq8o4:
                selected = 4;
                break;
        }

        if(selected == corbq8)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq8)
        {
            wrongAnsBio++;
        }

    }
}

现在的问题是,如果用户先选择了一个不正确的选项,然后将其更改为正确的选项,则错误和正确答案的指针都会递增。我在 RadioButton 的 XML 代码中使用了 onClick 属性。可能必须有一些其他属性来显示选项以获取最终选中的选项,而不是一次又一次地发生。

以下是 XML 代码:

<RadioGroup
     android:id="@+id/rgbq1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="14dp">

     <RadioButton
            android:id="@+id/rbbq1o1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:onClick="clickedBq1"
            android:text="Option1"
            android:textColor="@color/colorPrimaryVariant"
            android:textSize="20sp" />

您可以添加单选按钮更改侦听器

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // checkedId is the RadioButton selected
    }
})

或在下一次单击按钮时获取选定的单选按钮。如下图

    next.setOnClickListener(new OnClickListener(View v){
        String value =
            ((RadioButton)findViewById(rg.getCheckedRadioButtonId()))
            .getText().toString();
          // here you can compare the selected answer is  correct or not     
     });

你必须给出一个变量来检查你之前是否回答过(之前的答案是真还是假)。

方法示例 clickedBq1

public void clickedBq1(View v){
        int selected = -1;
        boolean statusAnswer = false
        switch(rgbq1.getCheckedRadioButtonId())
        {
            case R.id.rbbq1o1:
                selected = 1;
                break;
            case R.id.rbbq1o2:
                selected = 2;
                break;
            case R.id.rbbq1o3:
                selected = 3;
                break;
            case R.id.rbbq1o4:
                selected = 4;
                break;
        }

        if(statusAnswer == false)
        {
            if(selected == corbq1)
            {
                correctAnsBio++;
            }
            else if(selected >0 && selected != corbq1)
            {
                wrongAnsBio++;
            }
            statusAnswer == true
        }
        else
        {
            if(selected == corbq1)
            {
                correctAnsBio++;
                wrongAnsBio--;
            }
            else if(selected >0 && selected != corbq1)
            {
                wrongAnsBio++;
                correctAnsBio--;
            }
        }
    }