android 中的 RadioButton 值

RadioButton value in android

场景如下:

我在一个 activity 中提出了一些问题,每个答案都有一个点(0 到 3)。例如,如果用户 select 第一个,他得到 0 分,如果他 select 第二个,他将得到 1 分等(第三个 2 分,第四个 3 分)。

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.first_radiobutton) {
                totalPoint = totalPoint + 0;
            } else if (checkedId == R.id.secound_radiobutton) {
                totalPoint = totalPoint + 1;
            }else if (checkedId == R.id.third_radiobutton) {
                totalPoint = totalPoint + 2;
            }else if (checkedId == R.id.forth_radiobutton) {
                totalPoint = totalPoint + 3;
            }
        }
    });

我将 totalPoint 作为全局变量并将其初始化为 public static int totalPoint = 0; 。问题是,如果用户改变了主意,并且 select 对当前问题有另一个答案,totalPoint 变量会将分数添加到自身。例如假设如果用户点击第三个 radioButtontotalPoint 变量将为 2,因此如果用户改变主意并且 select 第四个 radioButtontotalPoint 必须是 3 而不是 5(2+3)。我怎样才能避免这种情况?

在 class

处声明一个 int 数组
 int arrValues[]={0,0,0,0}; ///size can be differ as per question length you can create dynamic too as per your requirement

并保持在问题计数器或位置上 int iPosition=0; ///在上一个和下一个处理它

然后在setOnCheckedChangeListener中添加如下代码

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.first_radiobutton) {
               arrValues[iPosition] = 0;
            } else if (checkedId == R.id.secound_radiobutton) {
                arrValues[iPosition] =  1;
            }else if (checkedId == R.id.third_radiobutton) {
               arrValues[iPosition]=  2;
            }else if (checkedId == R.id.forth_radiobutton) {
              arrValues[iPosition] =  3;
            }
        }
    });

最后一步从数组

中添加所有值

1. 使用数组 answers[] 存储特定 question.

的答案 points

2.onCheckedChanged() 中,检查选定的 RadioButton 并使用值在特定 question 下存储分配的 points currentQuestion.

3.通过addinganswers数组的values计算total点。

这是工作代码:

public class MainActivity extends AppCompatActivity {

    RadioGroup radioGroup;

    int totalPoint = 0;
    int answers[] = new int[10]; // Required for 10 questions (0 to 9)
    int currentQuestion = 0;

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

        radioGroup = (RadioGroup) findViewById(R.id.radio_group);

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

                // Store points of specific question using position currentQuestion
                if (checkedId == R.id.first_radiobutton) {
                    answers[currentQuestion] = 0;
                } else if (checkedId == R.id.second_radiobutton) {
                    answers[currentQuestion] = 1;
                }else if (checkedId == R.id.third_radiobutton) {
                    answers[currentQuestion] = 2;
                }else if (checkedId == R.id.forth_radiobutton) {
                    answers[currentQuestion] = 3;
                }

                // Total
                totalPoint = getTotalPoint();
            }
        });
    }

    public int getTotalPoint() {

        int sum = 0;
        for (int i = 0; i < answers.length; i++)
            sum += answers[i];

        return sum;
    }
}