【问题标题】:can i use sharedpreference in an other activity of my app我可以在我的应用程序的另一个活动中使用共享偏好吗
【发布时间】:2015-05-21 13:03:12
【问题描述】:

祝你有美好的一天。我想设置共享偏好活动。我已经设计了一个对话活动,但是如果我保存首选项,我可以从我的应用程序的其他活动中获取首选项。

@Override
public void pref(){

    SharedPreferences sharedpref =getSharedPreferences("BOYUT",Context.MODE_APPEND);
                        sharedpref=getSharedPreferences("THEME",Context.MODE_APPEND);
    SharedPreferences.Editor editor= sharedpref.edit();
     editor.putInt("BOYUT", sonboyut);
     editor.putInt("THEME",tema);
     editor.apply();
    Toast.makeText(getApplicationContext(),"Kaydedildi",Toast.LENGTH_LONG).show();

}

【问题讨论】:

    标签: android-intent android-activity sharedpreferences


    【解决方案1】:

    getSharedPreferencesgetDefaultSharedPreferences 就像文件打开命令。 apply() 就像关闭命令。此处第一次出现的 BOYUT 用作首选项文件的文件名,但接下来的几行,您将其用作要存储的值的名称。

    我想你想要的是,用getDefaultSharedPreferences 打开默认的 SharedPreference 文件,将两个值 BOYUT 和 THEME 放入其中,然后将 apply() 放入其中。然后,您可以从程序的其他部分通过getDefaultSharedPreferencesgetInt 访问这些值。

    您所做的是创建两个名为 BOYUT 和 THEME 的首选项文件,并将名为 BOYUT 和 THEME 的值放入后面,使第一个文件为空。我不确定您后来打开了哪一个来检索值。

    您可能想查看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      相关资源
      最近更新 更多