2

我无法理解共享偏好。我有用户将插入密码、起始价格、等待价格等的活动。我的计划是设置起始值,然后用户可以根据需要更改该值。我的问题是:如果我在 onCreate() 方法中创建首选项,那么每次运行应用程序时如何应用更改(使用 SharedPreferences.Editor)它应该在首选项中创建新值。

4

2 回答 2

1

不,它会一键更改前一个....

http://mobile.tutsplus.com/tutorials/android/android-application-preferences/

于 2012-05-23T17:39:48.643 回答
1

要获得共享偏好,请在您的活动中使用以下方法:

 SharedPreferences prefs = this.getSharedPreferences(
          "com.example.app", Context.MODE_PRIVATE);

阅读偏好:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

编辑和保存首选项

 Date dt = getSomeDate();
    prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();
于 2012-05-23T17:44:07.017 回答