获得单选按钮的最终状态

时间:2014-02-10 12:38:20

标签: java android radio-button

我的活动有一些单选按钮组,我想获得此单选按钮组的最终状态,并在类型为整数的列表中添加无线电ID。

但是我在选择选项1然后选择选项2时出现问题我将其添加到列表中的无线电ID中!但我想在用户转移到另一个活动时只存储最后的雕像

我正在尝试使用Ondestroy()

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

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch(checkedId){
                case R.id.RadioButton11:

                    BatteryActivity.this.Answer = 23 ;

                break;

                case R.id.RadioButton08:
                    BatteryActivity.this.Answer = 24 ;

                break;
            }//


        }
    });


}
@Override
protected void onDestroy() {

    ((MyApplication) BatteryActivity.this.getApplication()).setAnswerList(this.Answer);
    ((MyApplication) BatteryActivity.this.getApplication()).setSectionList(10);
}

1 个答案:

答案 0 :(得分:0)

将以下代码放在onStop()方法中。删除rediogroup的checkchangedListener方法和onDestroy()方法......

int radioButtonID = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton); // ID of selected radiobutton

switch(idx ){
     case R.id.RadioButton11:

           BatteryActivity.this.Answer = 23 ;

           break;

     case R.id.RadioButton08:
           BatteryActivity.this.Answer = 24 ;

           break;
     }