How to save state of radio button in android

后端 未结 1 956
轻奢々
轻奢々 2021-01-17 06:54

I want to save the state of radio button clicked in android. When user goes to next question it must save state so when he comes back to previous state it must show the sele

1条回答
  •  伪装坚强ぢ
    2021-01-17 07:36

    Use Shared Preferences, To save your radio button's state,and you get to retrieve data in other activities.

    this is java class:

    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    
    public class ABCD extends Activity{
    
    
        private int ival;
        public static int glob=1;
        static SharedPreferences sPref;
        private SharedPreferences.Editor sE;
        RadioGroup rbMain;
        RadioButton rb1,rb2,rb3,rb4,rb5;   
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
    
            rbMain = (RadioGroup) findViewById(R.id.rgMain);
            rb1 = (RadioButton)findViewById(R.id.radio1);
            rb2 = (RadioButton)findViewById(R.id.radio2);
            rb3 = (RadioButton)findViewById(R.id.radio3);
            rb4 = (RadioButton)findViewById(R.id.radio4);
            rb5 = (RadioButton)findViewById(R.id.radio5);
    
            sPref = getSharedPreferences("Pref",0);
            sE = sPref.edit();
            ival = sPref.getInt("Pref", 0);
    
            if(ival == R.id.radio1){
                rb1.setChecked(true);
                glob=1;
            }else if(ival == R.id.radio2){
                rb2.setChecked(true);   
                glob=2;
            }else if(ival == R.id.radio3){
                rb3.setChecked(true);
                glob=3;
            }else if(ival == R.id.radio4){
                rb4.setChecked(true);   
                glob=4;
            }else if(ival == R.id.radio5){
                rb5.setChecked(true);
                glob=5;
            }
    
            rbMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                int state = 0;
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == R.id.radio1){
                        sE.clear();
                        sE.putInt("Pref", checkedId);
                        sE.apply();
                    }else if(checkedId == R.id.radio2){
                        sE.clear();
                        sE.putInt("Pref", checkedId);
                        sE.apply();
                    }else if(checkedId == R.id.radio3){
                        sE.clear();
                        sE.putInt("Pref", checkedId);
                        sE.apply();
                    }else if(checkedId == R.id.radio4){
                        sE.clear();
                        sE.putInt("Pref", checkedId);
                        sE.apply();
                    }else if(checkedId == R.id.radio5){
                        sE.clear();
                        sE.putInt("Pref", checkedId);
                        sE.apply();
                    }
    
                }
            });
    
        }   
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            sPref = getSharedPreferences("Pref",0);
            sE = sPref.edit();
            ival = sPref.getInt("Pref", 0);
    
            if(ival == R.id.radio1){
                rb1.setChecked(true);
                glob=1;
            }else if(ival == R.id.radio2){
                rb2.setChecked(true);   
                glob=2;
            }else if(ival == R.id.radio3){
                rb3.setChecked(true);
                glob=3;
            }else if(ival == R.id.radio4){
                rb4.setChecked(true);   
                glob=4;
            }else if(ival == R.id.radio5){
                rb5.setChecked(true);
                glob=5;
            }
        }
    
    }
    

    and this is my XML Layout:

    
    
    
    
     
                     
            
                        
            
                        
            
                        
            
                        
            
        
    
    
    
    

    And, this can help you to do whatever you need.

    0 讨论(0)
提交回复
热议问题