Android - 即使用户重新启动应用程序,如何保持多个复选框的选中状态?

Android - How to keep the checked state of multiple checkboxes even if a user relaunches the app?

我正在 Android sdk 平台上工作。应用程序的工作 - 用户选择多个复选框,然后根据选择,用户将收到通知。当用户重新启动应用程序时,选中的复选框会保持选中状态。我的方法应该是什么?以下代码用于一个复选框。我想为多个复选框执行它。我该如何处理?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.arham.csdevbin.downloadimagefrominternet.SharedPreferencesDemo">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="103dp"
        android:layout_marginStart="103dp"
        android:layout_marginTop="136dp"
        android:text="CheckBox" />
</RelativeLayout>

MainActivity.java

 CompoundButton.OnCheckedChangeListener {
    CheckBox checkBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        checkBox = findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            checkBox.setText("The CheckBox is Checked");
        } else {
            checkBox.setText("The CheckBox is not Checked");
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences = getPreferences(0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("CHECK_BOX_VALUE", checkBox.getText().toString());
        editor.putBoolean("CH", checkBox.isChecked());
        editor.commit();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
        SharedPreferences sharedPreferences = getPreferences(0);
        boolean checkBoxValue = sharedPreferences.getBoolean("CH", false);
        String chBoxString = sharedPreferences.getString("CHECK_BOX_VALUE", "This is my CheckBox");
        checkBox.setChecked(checkBoxValue);
        checkBox.setText(chBoxString);
    }
}

您使用了 SharedPreference 并且只保存了一个复选框值。如果要保存多个复选框值,则必须将所有选中的复选框值存储在 SharedPreference.
我建议您保存多个值,使用 HashMap 获取和设置 SharedPreference 中的复选框。

SharedPreferences preferences;
public SharedPreferences.Editor editor;

public void setCheckboxes(HashMap<String, boolean> checkboxes) {

    editor.putString("checkbox_1", checkboxes.get("checkbox_1"));
    editor.putString("checkbox_2", checkboxes.get("checkbox_2"));
    editor.putString("checkbox_3", checkboxes.get("checkbox_3"));
    editor.putString("checkbox_4", checkboxes.get("checkbox_4"));
    editor.apply();
}

public HashMap<String, boolean> getCheckboxes() {
    HashMap<String, boolean> checkboxes = new HashMap<String, boolean>();
    checkboxes.put("checkbox_1", preferences.getString("checkbox_1", false));
    checkboxes.put("checkbox_2", preferences.getString("checkbox_2", false));
    checkboxes.put("checkbox_3", preferences.getString("checkbox_3", false));
    checkboxes.put("checkbox_4", preferences.getString("checkbox_4", false));

return checkboxes;

}