【问题标题】:Edit SharedPreferences value?编辑 SharedPreferences 值?
【发布时间】:2012-05-23 17:37:11
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案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();
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        • 2012-03-05
        • 2023-04-02
        • 2016-10-15
        • 1970-01-01
        • 2011-04-07
        • 2015-05-13
        相关资源
        最近更新 更多