Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 保存radiobutton的状态_Android_Sharedpreferences - Fatal编程技术网

Android 保存radiobutton的状态

Android 保存radiobutton的状态,android,sharedpreferences,Android,Sharedpreferences,我的应用程序中有5个单选按钮,我想保存它们的状态,这样当我退出并返回应用程序时,我会看到在退出应用程序之前单击的相同按钮 下面是我保存状态的代码 private int flag1 = true; private void save(final boolean isChecked) { SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Edi

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

下面是我保存状态的代码

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.
是否有其他方法使用SharedReferences???

在单独的助手类中使用SharedReferences。这样,您就可以在活动开始时初始化SharedReference,并检查其中是否存在任何值

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

您可以使用此代码

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>

使用如下共享首选项保存单选按钮值

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

您正在使用相同的keycheck保存所有按钮状态。试试这样的

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)); 
 }
}
并使用以下代码恢复

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

 }

您也可以尝试覆盖:

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

你总是保存支票吗?!每次应用程序暂停时,检查都保存为在code onPause方法sirSo中可见,正如我所怀疑的,您为所有单选按钮写入相同的值!如果你写支票是对的还是错的,你永远不会知道哪个是对的还是错的……好的,先生,我明白你想说的了
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");
}