Save the state of radiobutton

2019-09-22 02:36发布

问题:

I am having 5 radiobuttons in my application and i want to save their states so that when i exit and then come back to the application then i see the same button clicked which was clicked prior to my exiting the application

Following is my code for saving the state

private int flag1 = true;
private void save(final boolean isChecked) {

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

private boolean load() { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", true);
}

protected void onPause() {
    super.onPause();
    save(!flag1);
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
}

 @Override
 protected void onResume() {

    super.onResume();
    save(load());
        Toast.makeText(getApplicationContext(),""+load(), Toast.LENGTH_LONG).show();
}

I am calling the save method from inside the onCheckedChangeListener for every radiobutton but nothing happens.

Is there any other method of using sharedpreferences ??

回答1:

you are saving all button state using same key(check). Try something like this

private void save(int radioid,final boolean isChecked) {

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check"+radioid, isChecked);
editor.commit();
}

In load method loop through all radiobutton

private void load() { 
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
for(int i=0;i<radioGroup.getChildCount();i++){
   RadioButton rbtn=(RadioButton)radioGroup.getChildAt(i);
   rbtn.setChecked(sharedPreferences.getBoolean("check"+rbtn.getId(), false)); 
 }
}

UPDATE

In RadioGroup only one button is checked at a time. so store check button id instead.

private void save(int radioid) {

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("check", radioid);
editor.commit();
}

and use following code to restore

private void load() { 
   SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);

   int radioId=sharedPreferences.getInt("check", 0);
   if(radioId>0){
     RadioButton rbtn=(RadioButton)radioGroup.findViewById(radioId);
     rbtn.setChecked(true); 
   }

 }


回答2:

you can use this code

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
    SharedPreferences sp = getSharedPreferences("setting", MODE_PRIVATE);

    if(sp.getInt("checked", 0) != 0){

    rg.check(sp.getInt("checked", 0));

    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
    int id = rg.getCheckedRadioButtonId();
    SharedPreferences sp = getSharedPreferences("setting", MODE_PRIVATE);
    Editor e = sp.edit();
    e.putInt("checked", id);
    e.commit();
    }

}

and here is layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="53dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>



回答3:

You could also try override:

public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putBoolean("radioButton1", true);
    savedInstanceState.putBoolean("radioButton2", false);
    savedInstanceState.putBoolean("radioButtonN", false);
}

public void onRestoreInstanceState(Bundle savedInstanceState){
    boolean rb1_state = savedInstanceState.getBoolean("radioButton1");
    boolean rb2_state = savedInstanceState.getBoolean("radioButton2");
    boolean rbN_state = savedInstanceState.getBoolean("radioButtonN");
}

methode



回答4:

use SharedPreferences in a separate helper class. That way you could initialize the SharedPreferences in the start of the activity and check if there were any values present there.

You can see the code example here. Doing this ... as I said in the onCreate you could check if the SharedPreferences has any value. Check the full source code here.



回答5:

save your radio button value using shared preferences like this

    SharedPreferences settings = getSharedPreferences("LEVEL", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("FIRST", radiobutton1.isChecked()); // first argument is a                                                        

    editor.commit(); // Commit the changes

And to load this you have to set it to default true/false.

    SharedPreferences settings = getSharedPreferences("LEVEL", 0);
    boolean isChk= settings.getBoolean("FIRST", false);

and to use this write down below code

    if (isChk)
     radiobutton1.setChecked(true);