保存radiobutton的状态

时间:2013-12-30 11:18:55

标签: android sharedpreferences

我的应用程序中有5个radiobuttons,我想保存它们的状态,这样当我退出然后返回应用程序时,我看到在我退出应用程序之前单击的相同按钮

以下是我保存状态的代码

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.

还有其他使用sharedpreferences的方法吗?

5 个答案:

答案 0 :(得分:3)

您正在使用相同的键保存所有按钮状态(检查)。试试这样的事情

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

在加载方法中循环遍历所有单选按钮

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

<强>更新

在RadioGroup中,一次只检查一个按钮。 所以存储检查按钮ID。

private void save(int radioid) {

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

并使用以下代码恢复

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

 }

答案 1 :(得分:1)

您可以使用此代码

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

}

这是布局:

<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>

答案 2 :(得分:1)

您也可以尝试覆盖:

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

梅索德

答案 3 :(得分:0)

在单独的帮助程序类中使用SharedPreferences。这样你就可以在活动开始时初始化SharedPreferences并检查那里是否有值。

您可以看到代码示例here。这样做...正如我在onCreate中所说,您可以检查SharedPreferences是否有任何价值。查看完整的源代码here

答案 4 :(得分:0)

使用共享偏好设置保存您的单选按钮值

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

    editor.commit(); // Commit the changes

要加载它,您必须将其设置为默认值true / false。

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

并使用此代码下面的代码

    if (isChk)
     radiobutton1.setChecked(true);