【问题标题】:save state and value of radio button保存单选按钮的状态和值
【发布时间】:2014-01-15 13:36:51
【问题描述】:

在主要活动中,我有单选按钮,在方法 public void onRadioButtonClicked(View view) 中,我将每个选定的单选按钮与一个值相关联,所以我有两个变量 a 和 b,之后我单击按钮 bton i根据方法 ajouter,a 和 b 值进入数据库。直到现在一切正常,我决定使用 OnResume 和 OnPause 方法来保存活动状态,所以当我返回活动时,我看到单选按钮被选中,当我单击按钮保存信息时,问题就出现了,我得到 0 作为数据库中 a 和 b 的值,而不是我看到选择的值,奇怪的是,当我再次单击这些单选按钮时,然后在保存按钮,我得到正确的值!我的问题是如何在不再次单击所选单选按钮的情况下生成 a 和 b 的值?

    public class ActivityUn extends Activity {
        public void  ajouter(View v) {
          db.open();               
          db.insertMENAGE(a,b);                   
          db.close();
          Toast.makeText(getApplicationContext(), "Données Enregistrées",Toast.LENGTH_SHORT).show();
              } 

   @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_un);       

         Button bton = (Button)findViewById(R.id.ajoutUn);
         bton.setOnClickListener(new View.OnClickListener() {  

          @Override
             public void onClick(View v) {

                   ajouter(v);                                            }
         });

public void onRadioButtonClicked(View view) {
    // Is the button now checked?

        boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.rm_13_1:
                    if (checked)
                          a=1;
                        break;
                case R.id.rm_13_2:
                    if (checked)
                        a=2;
                        break;

                case R.id.rm_14_1:
                    if (checked)
                        b=1;

                    break;
                case R.id.rm_14_2:
                    if (checked)
                        b=2;
                       break;
                case R.id.rm_14_3:
                    if (checked)
                        b=3;
                    findViewById
                    break;
                case R.id.rm_14_4:
                    if (checked)
                        b=4;
                          break;
  }
}

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,
        MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs3.edit();
    editor.putBoolean("questionA", rm_13_1.isChecked());
    editor.putBoolean("questionB", rm_13_2.isChecked());
    editor.putBoolean("questionC", rm_14_1.isChecked());
    editor.putBoolean("questionD", rm_14_2.isChecked());
    editor.putBoolean("questionE", rm_14_3.isChecked());
    editor.commit();
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
    rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
    rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
    rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
    rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
    rm_14_3 = (RadioButton) findViewById(R.id.rm_14_2);
    Boolean rm_13_1A = false;
    Boolean rm_13_2A = false;
    Boolean rm_14_1A = false;
    Boolean rm_14_2A = false;
    Boolean rm_14_3A = false;
    rm_13_1A = prefs3.getBoolean("questionA", false);
    rm_13_2A = prefs3.getBoolean("questionB", false);
    rm_14_1A = prefs3.getBoolean("questionC", false);
    rm_14_2A = prefs3.getBoolean("questionD", false);
    rm_14_3A = prefs3.getBoolean("questionE", false);
    rm_13_1.setChecked(rm_13_1A);
    rm_13_2.setChecked(rm_13_2A);
    rm_14_1.setChecked(rm_14_1A);
    rm_14_2.setChecked(rm_14_2A);
    rm_14_3.setChecked(rm_14_3A);
}

【问题讨论】:

  • 我做了很多努力都没有找到解决办法,请帮忙!
  • @Binghammar:有没有办法解决这个问题??
  • “值为空”是什么意思?
  • 我确实将每个收音机与一个值相关联,然后我将值保存到数据库,问题是值不存在!!!
  • 在视觉上,单选按钮被选中,但它所代表的值不存在,你知道为什么吗?

标签: android android-layout sharedpreferences


【解决方案1】:

单选按钮应该只存在于单选按钮组中。单选按钮的特点是一次只能选择一组中的一个。如果用户选择 C,那么所有其他单选按钮都会被取消选择(想想一个多项选择题。您只能选择一个答案)如果您希望选择多个选项,请查看 android 的 CheckBox view

【讨论】:

  • 非常感谢您的回复,您说的是正确的,我知道,但这里的问题是关于如何在 OnResume 方法中获取单选按钮的值,而无需再次单击它们,因为我解释说我看到单选按钮被选中,但是当我点击保存按钮时,我没有得到值,例如变量 a
  • 我认为问题在于我如何使方法 public void onRadioButtonClicked(View view) 在 OnResume 中得到识别!
  • 所以当我自动点击保存按钮时,a 和 b 的值会采用 onRadioButtonClicked 中指定的值,这就是我要找的,再次感谢 :)
猜你喜欢
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多