SharedPreferences不能按要求工作。我要去哪里错了?点击时会保存SharedPreferences,同样,在我的Android活动中也会检索值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();
}
});
答案 0 :(得分:0)
为什么您似乎有两个不同的SwitchCompat
视图,分别称为mySwitch
和btn
?很难说出您要完成什么。
无论如何,请尝试使用此方法来获取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();
}
答案 1 :(得分:0)
发生这种情况是因为final boolean switchChecked = silent;
内的setOnClickListener
您在switchChecked
中保持沉默,因此沉默始终是错误的
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();
}
});