开关紧凑 Android SharedPreferences 不工作

Switch compact Android SharedPreferences is not working

以下代码中的 SharedPreferences 无法按要求工作。我哪里错了? SharedPreferences 在点击时保存,同样的是在我的 Android activity.

中检索 OnCreate 和 OnResume 的值

代码:

mySwitch = (SwitchCompat) findViewById(R.id.atten);
strttime = (TextView) findViewById(R.id.textView5);
endtime = (TextView) findViewById(R.id.textView6);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
silent = settings.getBoolean("switchkey", false);
mySwitch.setChecked(silent);

mySwitch.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        final SwitchCompat btn = (SwitchCompat) v;
        final boolean switchChecked = silent;

        String message = "Are you sure you want to Logout?";
        if (!btn.isChecked()) {
            message = "Are you sure you want to Login?";
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyAlertDialogStyle); // Change "this" to `getActivity()` if you're using this on a fragment
        builder.setMessage(message).setPositiveButton("Ok", new DialogInterface.OnClickListener() {@Override
            public void onClick(DialogInterface dialog, int i) {
                // "Yes" button was clicked
                if (switchChecked) {

                    Log.d("You are :", "Checked");

                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("switchkey", true);
                    editor.apply();
                } else {
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("switchkey", false);
                    editor.apply();
                }
            }
        }).setNegativeButton("Cancel", null).show();
    }
});

为什么您似乎有两个不同的 SwitchCompat 视图,称为 mySwitchbtn?很难说出你想要完成什么。

无论如何,试试这个来获取 SwitchCompat:

的状态
builder.setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("switchkey", mySwitch.isChecked());
                        //or maybe you want btn.isChecked()? I don't know
                        editor.apply();
                    }

发生这种情况是因为 final boolean switchChecked = silent; 在您的 setOnClickListener

你在 switchChecked 中传递了 silent,因此 silent 总是错误的 silent = settings.getBoolean("switchkey", false);(如果没有可用值)

所以您所要做的就是像这样在 switchChecked 中传递正确的值

final boolean switchChecked = btn.isChecked();

因为您需要检查用户是否按下了您的开关

现在 switchChecked 将根据用户输入得到正确的值

因此您的代码将是

  mySwitch = (SwitchCompat) findViewById(R.id.atten);
    strttime = (TextView) findViewById(R.id.textView5);
    endtime = (TextView) findViewById(R.id.textView6);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    silent = settings.getBoolean("switchkey", false);
    mySwitch.setChecked(silent);

    mySwitch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final SwitchCompat btn = (SwitchCompat) v;
            final boolean switchChecked = btn.isChecked();

            String message = "Are you sure you want to Logout?";
            if (!btn.isChecked()) {
                message = "Are you sure you want to Login?";
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage(message)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int i) {
                            // "Yes" button was clicked
                            if (switchChecked) {

                                Log.d("You are :", "Checked");

                                SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                                SharedPreferences.Editor editor = settings.edit();
                                editor.putBoolean("switchkey", true);
                                editor.apply();
                            } else {
                                SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                                SharedPreferences.Editor editor = settings.edit();
                                editor.putBoolean("switchkey", false);
                                editor.apply();
                            }

                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .show();

        }

    });