【问题标题】:RadioButton value in androidandroid中的RadioButton值
【发布时间】:2017-05-31 20:23:31
【问题描述】:

这是场景:

我在一项活动中提出了一些问题,每个答案都有一个点(0 到 3)。例如,如果用户选择第一个,他得到 0 分,如果他选择第二个,他将得到 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; 。问题是如果用户改变了主意并为当前问题选择了另一个答案,totalPoint 变量会将这个点添加到自身。例如假设如果用户点击第三个radioButtontotalPoint 变量将为 2,所以如果用户改变主意并选择第四个radioButtontotalPoint 必须为 3,但它将是 5(2+3)。我怎样才能避免这种情况?

【问题讨论】:

  • 总是有 4 个按钮还是动态的?
  • 尝试保存最后选择的位置或radiogroup的radiogroupid(默认= -1),当你选择新的尝试删除与最后选择的位置相关的点...
  • @Pavan 每个问题都有 4 个可用答案。
  • 为什么不在Next按钮点击中添加值?
  • 您必须将点存储在数组(列表)中。索引是问题的编号。如果用户想更改他对预览问题的回答,您必须更改数组(列表)中索引处的点 = 问题的编号。

标签: android radio-button radio-group android-radiobutton


【解决方案1】:

1. 使用数组answers[] 存储特定question 的答案points

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

3.通过addingvaluesanswers数组计算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;
    }
}

【讨论】:

  • 谢谢。在这种情况下,如何在 sharedpreference 中保存 RadioButtons 的状态。可能吗?因为一次在一个活动中只有 4 个单选按钮。
  • 是的,您可以将此值存储在共享首选项中。好的,我会尽力回答你的问题
  • 如果我的回答看起来有用,希望你能投票。在此先感谢:)
【解决方案2】:

在类中声明一个 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;
            }
        }
    });

最后一步是从数组中获取所有值的加法

【讨论】:

    猜你喜欢
    • 2021-01-21
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多