多个RadioGroup onCheckedChanged

时间:2018-10-23 16:13:16

标签: android android-radiogroup

这是我第一次参加广播团体。我试图将值从3组保存到共享的首选项。它可以在一组中正常工作,但是一旦我尝试再添加2个,所有pref值都只是表格中的最后一组。我不确定该如何使用多个RadioGroup。任何帮助,将不胜感激。

onCreate

RadioGroup radioGroupGender = (RadioGroup) findViewById(R.id.gender_radio);
radioGroupGender.setOnCheckedChangeListener(this);

RadioGroup radioGroupActivity = (RadioGroup) findViewById(R.id.activity_level);
radioGroupActivity.setOnCheckedChangeListener(this);

RadioGroup radioGroupStatus = (RadioGroup) findViewById(R.id.current_status);
radioGroupStatus.setOnCheckedChangeListener(this);

还有我的onCheckedChanged

public void onCheckedChanged(RadioGroup radioGroup, int i) {
        int genderId = radioGroup.getCheckedRadioButtonId();
        RadioButton genderBut = (RadioButton)radioGroup.findViewById(genderId);
        gender_radio = genderBut.getText().toString();

        int activityLvl = radioGroup.getCheckedRadioButtonId();
        RadioButton activityLvlBut = (RadioButton)radioGroup.findViewById(activityLvl);
        activity_level = activityLvlBut.getText().toString();

        int currentStat = radioGroup.getCheckedRadioButtonId();
        RadioButton CurrentBut = (RadioButton)radioGroup.findViewById(currentStat);
        current_status = CurrentBut.getText().toString();
    }

1 个答案:

答案 0 :(得分:0)

您使用的是相同的gender_radio activity_level current_status,它们在每次onCheckedChanged()调用时都会被覆盖,只有最后一次调用onCheckedChanged()的值会被覆盖被保存