- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的项目中使用了一些 Activity 。其中之一是从 PreferenceActivity 扩展而来的。我有一个 CheckBoxPreference 和一个 EditTextPreference,我可以使用以下方法从所有其他 Activity 中获取值:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
pref = sharedPrefs.getString("edit_text_pref", "error");
但是当我打开我的应用程序时,这些值必须设置为默认值,因此我正在使用:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().clear().commit();
在我的主要 Activity 中。一切都应该没问题,但如果我在从首选项菜单中设置它之前尝试获取首选项,我会得到“错误”,而不是我在 xml 首选项文件中设置的默认值。如果我评论 getDefaultSharedPreferences
行它工作正常但显然当我打开应用程序时我得到旧的偏好。我怎样才能绕过这个问题?
最佳答案
你必须使用这个 setDefaultValues (Context context, int resId, boolean readAgain)
类似的东西
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
好好看看参数中的注释部分。这是关于再次阅读参数
context
共享首选项的上下文。
resId
首选项 XML 文件的资源 ID。
KEY_HAS_SET_DEFAULT_VALUES
为 false)时,此方法才会设置默认值。要尝试绕过此检查再次设置默认值,请将 readAgain 设置为 true。getDefaultSharedPreferences(Context)
并清除它,然后调用此方法并将此参数设置为 true所以我想在清除它之后你需要通过将 readAgain 传递为 true 将其设置为默认值
有点像
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().clear().commit();
PreferenceManager.setDefaultValues(this, R.xml.preference, true);
关于Android sharedPreferences设置默认值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856958/
我想知道以跨平台方式操作应用程序设置的最佳解决方案是什么。 在 iOS 中,我们可以在设置屏幕中更改应用程序外部的设置,但在 windows phone 和 android 中我们没有。 所以,我的想
我想清除除一个 SharedPreference 之外的所有内容。如果我已经保存了 10 个以上,有没有比单独删除每个更好的方法?这有点多余: preferences.edit().remove("1
我在名为 page1.dart 的页面中存储了一个值。我想从 page2.dart 或 page3.dart 访问存储的值。我怎样才能做到这一点? 最佳答案 Flutter 共享首选项实际上是作为内存
是否可以将 SharedPreferences 文件保存到自定义目录中?让我们说到 /data/data/package.name/my_prefs/。 或者是否可以检索默认保存 SharedPref
我很好奇存储库在 MVVM 架构中的作用。如果您决定将存储库添加到您的项目中,这个存储库是否只负责来自数据库或网络的数据?问题是关于 SharedPreferences 或 Files,我应该让存储库
尝试从服务创建共享首选项文件时出现以下错误: “无法为 SharedPreferences 文件/dbdata/databases/dimappers.android.pub/shared_prefs
所以我有这行代码 SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
This question already has answers here: What is a NullPointerException, and how do I fix it? (12个答案)
有谁知道 android 是否支持在一个项目中编译的多个 Android 模块之间共享相同的 sharedpreference? 我有两个共享首选项,目前当我尝试从当前模块外部的共享首选项访问一些数据
我正在搜索最大的 Android SharedPreferences 键值对,但找不到任何好的答案。其次,我想问一下,如果我有一个键,它的字符串值限制是多少。多少字符可以放入其中。如果我需要频繁更改值
我正在制作一个简单的预算跟踪应用程序。我有一个预算类,其中存储了一些变量:预算的金额、持续时间和初始值。我在 fragment 中只有一个全局预算对象,称为“预算”,我正在尝试保存它。它似乎保存得很好
我在 Flutter 应用程序中使用了 shared_preferences 插件,我想在用户选择城市时保存数据。 但是当我尝试打印它时,它只是说; Instance of 'SharedPrefer
我阅读了有关在 Flutter 应用程序中初始化 SharedPreferences 的其他答案(例如 this one ),但我想采取另一条路线:一开始就初始化 SharedPreferences
在我的 flutter 应用程序中,我实现了一个入职 View 。因为它应该只加载一次,所以我使用共享首选项来存储一个整数来表示已经显示了入职。当我在 Debug模式下运行应用程序时,一切正常。但是当
如果您使用 apply,在单独的线程中编辑共享首选项是否多余? 我的 MainActivity 的 onCreate 方法中有以下代码块: final MainActivity activit
我有 2 个 Activity ,身份验证和主页。 在身份验证中,它检查用户是否已登录,如果用户已登录,则将其重定向到Mainpage.class。这就是身份验证检查用户是否登录并重定向的方式。 Sh
为什么 toast 只显示空字符串或我在 getString 行上输入的“默认”值 @Override protected void onCreate(Bundle savedInstanceStat
我在我的项目中创建了一个模块。该模块的名称为“连接器”。现在我在 MainActivity 中创建 private SharedPreferences sPref; private S
刚接触 Java,很抱歉我的理解不够。我遇到了一个小障碍,我有一个用于连接到服务器的静态类。我正在为 SharedPreferences 使用另一个类,SharedPreferences 的一些详细信
这是一件好事。我将 int 值存储在我的共享首选项中,本质上是缓存它们,以便我可以在下次启动时将它们放入数据库中。我有一种方法可以在启动时将它们保存到数据库中。 接下来是从共享首选项中删除值以重置它们
我是一名优秀的程序员,十分优秀!