[英]My settings activity (using shared preferences) is not saving data, it is showing default values only
我目前正在開發一個 android 應用程序,為此我創建了一個設置活動。 我使用了共享首選項,但它沒有按預期工作。 我打開設置頁面,切換開關,從 memory 中刪除應用程序,但是當我再次打開它時,會顯示默認設置。 我不知道為什么。 請幫忙。 另外,我是初學者,因為我最近才開始編碼,所以如果我犯了一個愚蠢的錯誤,請原諒我。
public class SettingsActivity extends AppCompatActivity {
public static final String SETTINGS_PREFERENCES = "com.example.taskmasterv3.SettingsPreferences";
Switch switchReminder, switchNotifications;
boolean reminders;
boolean notifications;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
switchReminder = findViewById(R.id.switchReminder);
switchNotifications = findViewById(R.id.switchNotifications);
SharedPreferences prefs = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE);
boolean reminder = prefs.getBoolean("reminders",true );
boolean notification = prefs.getBoolean("notifications", false);
switchReminder.setChecked(reminder);
switchNotifications.setChecked(notification);
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}
}
}
我得到了你得到錯誤的地方。 您實際上無法設置該數據。
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}
它在您啟動活動時直接調用。 但是,這個 function 並不要求改變開關的 state。
所以,你必須像下面的源代碼一樣添加一個監聽器。
switchReminder.setOnClickListener(new View.OnClickListener() {
//enter your code here
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
})
在switchNotifications
上添加setOnClickListener()
。 比,我希望它會奏效。
編輯:
//setOnClickListener
switchReminder.setOnClickListener(new View.OnClickListener() {
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
})
那只是switchRemider
。 並且,以下源代碼用於switchNotification
。
//setOnClickListener
switchNotifications.setOnClickListener(new View.OnClickListener() {
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}
})
您已經添加了兩個代碼而不是
if (switchReminder.isChecked())
{
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.apply();
}
if (switchNotifications.isChecked())
{
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.apply();
}
好的,所以我終於讓它工作了。 我剛剛添加了一個保存按鈕來保存我的設置。 現在它就像一個魅力。 這是代碼:
public static final String SETTINGS_PREFERENCES = "com.example.taskmasterv3.SettingsPreferences";
SwitchCompat switchReminder, switchNotifications;
Button btnSaveSettings;
boolean remind;
boolean notify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
switchReminder = findViewById(R.id.switchReminder);
switchNotifications = findViewById(R.id.switchNotifications);
btnSaveSettings = findViewById(R.id.btnSaveSettings);
SharedPreferences preferences = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE);
boolean reminder = preferences.getBoolean("remind", true);
boolean notification = preferences.getBoolean("notify", false);
if (reminder)
{
switchReminder.setChecked(true);
}
if (notification)
{
switchNotifications.setChecked(true);
}
btnSaveSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
if (switchReminder.isChecked())
{
remind = true;
}
else
{
remind = false;
}
if (switchNotifications.isChecked())
{
notify = true;
}
else
{
notify = false;
}
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("remind", remind);
editor.putBoolean("notify", notify);
editor.commit();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.