我想了解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,但我不明白这个过程。
有人可以帮助我吗?
感谢您的时间
感谢您的评论,我认为这是我需要的,但可以从文件加载此默认值吗?或者在代码中的其他地方? –
当然。您可以将默认值编码为XML或文本文件,然后在您的活动中读取它。您也可以将您的默认值定义为另一个类中的变量并使用这些变量,可能将它们作为参数传递。 – crocboy
好的,我会试试看。谢谢 –