【问题标题】:Getting final status for radio button获取单选按钮的最终状态
【发布时间】:2014-02-10 20:45:04
【问题描述】:

我有一些单选按钮组的活动,我想获得这个单选组的最终状态并将单选 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);
}

【问题讨论】:

  • 而不是onDestroy() 使用onStop()
  • 不明白为什么只在用户更改活动时才需要存储,您可以为此更新一个全局变量

标签: java android radio-button


【解决方案1】:

将以下代码放入您的 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;
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2013-04-21
    • 2013-12-06
    • 2019-08-21
    • 2012-12-15
    • 2019-03-09
    相关资源
    最近更新 更多