微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的设置活动使用共享首选项未保存数据,仅显示默认值

如何解决我的设置活动使用共享首选项未保存数据,仅显示默认值

我目前正在开发一个 Android 应用程序,为此我创建了一个设置活动。我使用了共享首选项,但它没有按预期工作。我打开设置页面,切换开关,从内存中删除应用程序,但是当我再次打开它时,会显示认设置。我不知道为什么。请帮忙。另外,我是初学者,因为我最近才开始编码,所以如果我犯了一个愚蠢的错误,请原谅我。

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();

    }

它在您启动活动时直接调用。但是,这个函数不会调用改变开关的状态。

因此,您必须添加如下源代码的侦听器。

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();
}

})

还要在 setOnClickListener() 上添加 switchNotifications。比,我希望它会奏效。

编辑:

//setOnClickListener 
switchReminder.setOnClickListener(new View.OnClickListener() {
if (switchReminder.isChecked())
{
    reminders = true;
    SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES,reminders);
    editor.apply();
}

})

那只是为了switchRemider。并且,以下源代码适用于 switchNotification

//setOnClickListener 
switchNotifications.setOnClickListener(new View.OnClickListener() {
if (switchNotifications.isChecked())
    {
        notifications = true;
        SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES,notifications);
        editor.apply();

    }

})

您已添加两个代码而不是

if (switchReminder.isChecked())
    {
        reminders = true;
        SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES,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();
            }
        });
























    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?