Android 首选项:如何在用户未使用首选项屏幕时加载默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2691772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:58:12  来源:igfitidea点击:

Android Preferences: How to load the default values when the user hasn't used the preferences-screen?

androiddefaultpreferences

提问by Peterdk

I am using a PreferenceActivity to let the user set some values. I am feeding it the xml file with the defined preferences.

我正在使用 PreferenceActivity 让用户设置一些值。我正在向它提供具有定义首选项的 xml 文件。

I have set all the android:defaultValue=""for them.

我已经android:defaultValue=""为他们设置了所有。

When I start my application, I need the preferences, or if they are not set yet manually, I want the default values:

当我启动我的应用程序时,我需要首选项,或者如果它们尚未手动设置,我想要默认值:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean value = prefs.getBoolean("key"), false); 

However, when android:defaultValue="true"I still get false. So, it looks like the defaultValues set in the XML are not used anywhere but when initializing the preferences-screen.

但是,当android:defaultValue="true"我仍然得到false. 因此,看起来在 XML 中设置的 defaultValues 没有在任何地方使用,但在初始化首选项屏幕时除外。

I don't want to hardcode the default values in the getBoolean()method. So, is there a way get the default-values with only defining these in 1 place?

我不想在getBoolean()方法中硬编码默认值。那么,有没有办法只在 1 个地方定义这些默认值?

回答by pixel

this question is similar to mine:

这个问题和我的很相似:

initialize-preferences-from-xml-in-main-activity

初始化首选项来自 xml-in-main-activity

Just use this code in onCreatemethod:

只需在onCreate方法中使用此代码:

PreferenceManager.setDefaultValues(this, R.xml.preference, false);

It will load your preferences from XML, and last parameter (readAgain) will guarantee that user preferences won't be overwritten.

它将从 XML 加载您的首选项,最后一个参数 ( readAgain) 将保证不会覆盖用户首选项。

Take a look into PreferenceManager.setDefaultValuesin Android API for further investigation.

查看Android API 中的PreferenceManager.setDefaultValues以进行进一步调查。

回答by Francesco Vadicamo

Be aware that if you are using
getSharedPreferences(String sharedPreferencesName, int sharedPreferencesMode)

请注意,如果您正在使用
getSharedPreferences(String sharedPreferencesName, int sharedPreferencesMode)

to retrieve preferences you have to use
PreferenceManager.setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)
to set defaults!

检索您必须 用来设置默认值的首选项!
PreferenceManager.setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)

For example:
PreferenceManager.setDefaultValues(this, PREFS_NAME, Context.MODE_PRIVATE, R.xml.preference, false);

例如:
PreferenceManager.setDefaultValues(this, PREFS_NAME, Context.MODE_PRIVATE, R.xml.preference, false);

I hope this can help someone.

我希望这可以帮助某人。

回答by Steve Waring

in Pixel's accepted answer:

在 Pixel 接受的答案中:

PreferenceManager.setDefaultValues(this, R.xml.preference, false);

it is stated that the falsemeans that defaults won't be overwritten. This is not what it does, it is just an efficiency flag to stop the parsing if your application has more than one entry point. Unfortunately the test is not made per preference file, so if you have more than one preference file you must code trueon all but the first.

据说这false意味着不会覆盖默认值。这不是它的作用,如果您的应用程序有多个入口点,它只是一个停止解析的效率标志。不幸的是,测试不是针对每个首选项文件进行的,因此如果您有多个首选项文件,则必须true对除第一个之外的所有首选项文件进行编码。

If you are worried about efficiency, you could code something like this.

如果您担心效率,您可以编写这样的代码。

final static private int SPL = 1;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (sp.getInt("spl", 0) != SPL)
{
    PreferenceManager.setDefaultValues(this, R.xml.prefs1, true);
    PreferenceManager.setDefaultValues(this, R.xml.prefs2, true);
    sp.edit().putInt("spl", SPL).apply();
}

If you ever add more shared preferences, just set SPL to a hight number.

如果您添加了更多共享首选项,只需将 SPL 设置为一个最高数字。

回答by Macarse

For example extending DialogPreferenceI do this:

例如扩展DialogPreference我这样做:

@Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
    super.onSetInitialValue(restore, defaultValue);

    if (restore) {
        mValue = shouldPersist() ? getPersistedString(mDefault) : mDefault;
    } else {
        mValue = mDefault;
    }
}

mDefault can be:

mDefault 可以是:

  • mContext.getResources().getString(attrs.getAttributeResourceValue(androidns,"defaultValue", 100));
  • something you have indexed in R.
  • mContext.getResources().getString(attrs.getAttributeResourceValue(androidns,"defaultValue", 100));
  • 你在 R 中索引的东西。

回答by wirthra

Also make sure you have never used the SharedPreferences before. To make sure they are not changed (which means setDefaultValues(this,xml,false) has no effect) uninstall your App and upload it again to be sure no values are touched. This helped me.

还要确保您以前从未使用过 SharedPreferences。为确保它们不被更改(这意味着 setDefaultValues(this,xml,false) 无效)卸载您的应用程序并再次上传它以确保没有值被触及。这对我有帮助。