java 如何在用户离开应用Android Studio后存储复选框状态

fkvaft9z  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(69)

我想在AlertDialog中存储复选框状态,即使用户离开应用程序。
我该怎么做?
我尝试通过SharedPreferences,但它不能让它工作。

private AlertDialog showAlertDialog() {

        ArrayList<Integer> selectedItems = new ArrayList<>();
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        alertDialog.setTitle("Selecione o dia");
        String[] days = {"Domingo","Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira",
        "Sexta-Feira","Sabado"};
        boolean[] checkedItems = {false, false, false, false, false, false, false};
        alertDialog.setMultiChoiceItems(days, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i, boolean isChecked) {
            
            }
        });

        alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                Toast.makeText(MainActivity.this, "Dia(s) registrado(s)", Toast.LENGTH_LONG).show();

            }
        });
        alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

            }
        });
        AlertDialog alert = alertDialog.create();
        alert.setCanceledOnTouchOutside(false);
        alert.show();
        return alertDialog.create();

                }
wrrgggsh

wrrgggsh1#

首先,在你的代码中没有保存用户选择的部分,所以不清楚你是如何在SharedPreferences中保存它的。
你可以试试这个:
1.当用户单击Ok时,创建一个包含用户选择的字符串并将其保存在SharedPreferences中。

alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        String checkedDays = "";
        for (int i = 0; i < days.length; i ++) {
            if (checkedItems[i]) {
                checkedDays += days[i] + ",";
            }
        }

        getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE)
                .edit()
                .putString("Days", checkedDays)
                .apply();

        Toast.makeText(MainActivity.this, "Dia(s) registrado(s)", Toast.LENGTH_LONG).show();

    }
});

1.在显示AlertDialog之前读取预先保存的选择

String[] days = {"Domingo","Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira",
        "Sexta-Feira","Sabado"};
boolean[] checkedItems = {false, false, false, false, false, false, false};

SharedPreferences  preferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
String checkedDays = preferences.getString("Days", "");
for (int i = 0; i < days.length; i ++) {
    if (checkedDays.contains(days[i])) {
        checkedItems[i] = true;
    }
}
yhqotfr8

yhqotfr82#

使用SharedPreferences official docs为什么它在你的终端不起作用?
要写入共享首选项文件,请通过调用SharedPreferences上的edit()创建SharedPreferences.Editor。

注意:通过调用EncryptedSharedPreferences对象而不是SharedPreferences对象上的edit()方法,可以更安全地编辑共享首选项。要了解更多信息,请参阅如何更安全地使用数据的指南。

以下是通用共享首选项片段:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score_key), newHighScore);
editor.apply();

检查下面链接中的代码,并将您的逻辑调整为布尔和复选框:
https://www.tutorialspoint.com/android/android_shared_preferences.htm

相关问题