save state and value of radio button

笑着哭i 提交于 2019-12-31 05:25:07

问题


in the main activity i have radio buttons , in the method public void onRadioButtonClicked(View view) i associate each radio button selected with a value, so i have two variables a and b, after that i click on the button bton i have according to the method ajouter, a and b values into database.Until now everything is okey, i decide to use OnResume and OnPause methods to save the state of activity, so when i return to the activity i see that the radio buttons are selected, the problem comes when i click on button to save info , i get 0 as value for a and b in database instead of values that i saw selected, and the weird thing is that when i click again on thoses radio buttons and after that on save button, i get the right values ! My question here is how to make values of a and b without clicking again on the selected Radio Buttons ?

    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);
}

回答1:


Radio buttons should only exist in a radio button group. The characteristics of radio buttons is that only one in a group can be selected at a time. If a user selects C then all the other radio buttons become deselected (Think of a multiple choice question. You can only select one answer) If you're looking to have multiple options selected, look into android's CheckBox view



来源:https://stackoverflow.com/questions/20809530/save-state-and-value-of-radio-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!