共享首选项的android默认值

共享首选项的android默认值,第1张

概述我想了解 Android的SharedPreferences.我是初学者 并且对此不太了解. 我有我为我的应用程序首选项实现的这个类 public class Preferences { public static final String MY_PREF = "MyPreferences"; private SharedPreferences sharedPreferences 我想了解 Android的SharedPreferences.我是初学者
并且对此不太了解.

我有我为我的应用程序首选项实现的这个类

public class Preferences {    public static final String MY_PREF = "MyPreferences";    private SharedPreferences sharedPreferences;    private Editor editor;    public Preferences(Context context) {        this.sharedPreferences = context.getSharedPreferences(MY_PREF,0);        this.editor = this.sharedPreferences.edit();    }    public voID set(String key,String value) {        this.editor.putString(key,value);        this.editor.commit();    }    public String get(String key) {        return this.sharedPreferences.getString(key,null);    }    public voID clear(String key) {        this.editor.remove(key);        this.editor.commit();    }    public voID clear() {        this.editor.clear();        this.editor.commit();    }}

问题是我想设置默认首选项.它们将在安装应用程序时设置,并且可以在应用程序之后进行修改并保持持久性.
我听说过一个preferences.xml,但我不明白这个过程.

有人能帮助我吗?

谢谢你的时间

解决方法 很简单,如果你想为每个变量分别设置一个默认值,你需要为每个变量做一个,但是你的方法:

public String get(String key) {    return this.sharedPreferences.getString(key,"this is your default value");}

如果用户从未访问过变量或从未创建变量,系统会将默认值设置为值,如果您或用户更改了此值,则忽略默认值.见http://developer.android.com/guide/topics/data/data-storage.html#pref

直接来自AndroID文档:

The SharedPreferences class provIDes a general framework that allows you to save and retrIEve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans,floats,ints,longs,and strings. This data will persist across user sessions (even if your application is killed).

总结

以上是内存溢出为你收集整理的共享首选项的android默认值全部内容,希望文章能够帮你解决共享首选项的android默认值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129917.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存