几天来我一直在尝试正确实现 onSaveInstanceState()
方法,但直到现在都没有成功。
我做错了什么?我的代码没有返回错误,但复选框的状态没有保存?
是否可以实现onSaveInstanceState()
方法?
如果我使用 SharedPreferences
会更好吗?
我想要的是在旋转屏幕时不要丢失复选框选择。如果有人能纠正我正在做的事情,我将不胜感激!
这是我的:
MainActivity.java:
package com.example.alex.savepreferencescheckbox;
import android.annotation.SuppressLint;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AlertDialog dialog;
CheckBox cbAllDay, cbBefore12, cbBetween1216, cbBetween1620, ccbAfter20;
Boolean myBoolean1, myBoolean2, myBoolean3, myBoolean4, myBoolean5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void alertDialog(View view) {
if (dialog == null) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
@SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
builder.setView(checkBoxView);
cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);
dialog = builder.create();
}
dialog.show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("CheckBox1", cbAllDay.isChecked());
outState.putBoolean("CheckBox2", cbBefore12.isChecked());
outState.putBoolean("CheckBox3", cbBetween1216.isChecked());
outState.putBoolean("CheckBox4", cbBetween1620.isChecked());
outState.putBoolean("CheckBox5", ccbAfter20.isChecked());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myBoolean1 = savedInstanceState.getBoolean("CheckBox1");
myBoolean2 = savedInstanceState.getBoolean("CheckBox2");
myBoolean3 = savedInstanceState.getBoolean("CheckBox3");
myBoolean4 = savedInstanceState.getBoolean("CheckBox4");
myBoolean5 = savedInstanceState.getBoolean("CheckBox5");
}
public void applyFilter(View view) {
dialog.dismiss();
if (cbAllDay.isChecked()){
Toast.makeText(this, "The first box was checked", Toast.LENGTH_SHORT).show();
} else if (cbBefore12.isChecked()){
Toast.makeText(this, "The second box was checked", Toast.LENGTH_SHORT).show();
} else if (cbBetween1216.isChecked()){
Toast.makeText(this, "The third box was checked", Toast.LENGTH_SHORT).show();
} else if (cbBetween1620.isChecked()){
Toast.makeText(this, "The forth box was checked", Toast.LENGTH_SHORT).show();
} else if (ccbAfter20.isChecked()){
Toast.makeText(this, "The fifth box was checked", Toast.LENGTH_SHORT).show();
}
}
public void doNothing(View view) {
dialog.dismiss();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.alex.savepreferencescheckbox.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_marginTop="71dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:onClick="alertDialog"/>
</RelativeLayout>
markers_filtering.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Your Hour!"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center"
android:id="@+id/title_hour_selection"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title_hour_selection"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="All day long" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="Before 12 P.M."
android:layout_below="@+id/checkBox1"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox2"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="Between 12.00 P.M. - 16.00 P.M." />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox3"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="Between 16.00 P.M. - 20.00 P.M." />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_below="@+id/checkBox4"
android:text="After 20.00 P.M."
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginBottom="30dp"
android:layout_below="@+id/checkBox5"
android:id="@+id/okButton"
android:onClick="applyFilter"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancelButton"
android:layout_below="@+id/checkBox5"
android:layout_alignRight="@+id/checkBox5"
android:layout_alignEnd="@+id/checkBox5"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:onClick="doNothing"/>
</RelativeLayout>
最佳答案
新编辑:
我做了一个与您类似的项目并对其进行了测试。您的代码看起来不错,但您唯一忘记的是在 alertDialog()
方法中使用 onRestoreInstanceState()
提供的保存值设置您的复选框方法。
这里是MainActivity.java
的完整代码:
package com.example.alex.savepreferencescheckbox;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AlertDialog dialog;
CheckBox cbAllDay, cbBefore12, cbBetween1216, cbBetween1620, ccbAfter20;
Boolean myBoolean1, myBoolean2, myBoolean3, myBoolean4, myBoolean5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void alertDialog(View view) {
if (dialog == null) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
@SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
builder.setView(checkBoxView);
cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
if (myBoolean1 != null) {
cbAllDay.setChecked(myBoolean1);
}
cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
if (myBoolean2 != null) {
cbBefore12.setChecked(myBoolean2);
}
cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
if (myBoolean3 != null) {
cbBetween1216.setChecked(myBoolean3);
}
cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
if (myBoolean4 != null) {
cbBetween1620.setChecked(myBoolean4);
}
ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);
if (myBoolean5 != null) {
ccbAfter20.setChecked(myBoolean5);
}
dialog = builder.create();
}
dialog.show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("CheckBox1", cbAllDay.isChecked());
outState.putBoolean("CheckBox2", cbBefore12.isChecked());
outState.putBoolean("CheckBox3", cbBetween1216.isChecked());
outState.putBoolean("CheckBox4", cbBetween1620.isChecked());
outState.putBoolean("CheckBox5", ccbAfter20.isChecked());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myBoolean1 = savedInstanceState.getBoolean("CheckBox1");
myBoolean2 = savedInstanceState.getBoolean("CheckBox2");
myBoolean3 = savedInstanceState.getBoolean("CheckBox3");
myBoolean4 = savedInstanceState.getBoolean("CheckBox4");
myBoolean5 = savedInstanceState.getBoolean("CheckBox5");
}
public void applyFilter(View view) {
dialog.dismiss();
if (cbAllDay.isChecked()) {
Toast.makeText(this, "The first box was checked", Toast.LENGTH_SHORT).show();
} else if (cbBefore12.isChecked()) {
Toast.makeText(this, "The second box was checked", Toast.LENGTH_SHORT).show();
} else if (cbBetween1216.isChecked()) {
Toast.makeText(this, "The third box was checked", Toast.LENGTH_SHORT).show();
} else if (cbBetween1620.isChecked()) {
Toast.makeText(this, "The forth box was checked", Toast.LENGTH_SHORT).show();
} else if (ccbAfter20.isChecked()) {
Toast.makeText(this, "The fifth box was checked", Toast.LENGTH_SHORT).show();
}
}
public void doNothing(View view) {
dialog.dismiss();
}
}
这是模拟器的屏幕截图(Galaxy Nexus API 23,它也适用于 API 16,我已经测试过了):
人像模式:
横向模式:
关于android - 多个复选框的 onSaveInstanceState 或 SharedPreferences?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36845457/