为什么我的首选项的默认值没有保存到 SharedPreferences?

Why is the defaultValue of my preference not being saved to SharedPreferences?

在下面的SSCCE中,思路是在主activity中有一个按钮,当用户点击它时,(调用方法showCheckBoxPreferencesValue())的默认值只有 preferences.xml 中定义的首选项(使用 android:default_value)应显示在此按钮下方的文本框中。

为了让这个默认值显示在文本框中,我使用方法 sharedPreferences.getString() 并将 Preference does not exist 作为它的第二个参数传递,这意味着如果首选项(使用 key 作为第一个参数传递)似乎不存在,则字符串 Preference does not exist 被设置为文本框。 (Source)

这是真的吗?我哪里做错了?请看最后的截图。

res/xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <EditTextPreference android:key="@string/preferences_editTextPreference_key"
        android:title="@+id/preferences_editTextPreference_title"
        android:defaultValue="@string/preferences_editTextPreference_defaultValue" />

</PreferenceScreen>

MainActivity.java:

public class MainActivity extends Activity {
    private String EDIT_TEXT_PREFERENCE_KEY;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.mainActivity_textView);

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

    }

    public void showCheckBoxPreferencesValue(View view) {

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        EDIT_TEXT_PREFERENCE_KEY = getResources().getString(R.string.preferences_editTextPreference_key);

        String editTextPreferenceValue = sharedPreferences.getString(EDIT_TEXT_PREFERENCE_KEY,
                "Preference does not exist");

        textView.setText("Checkbox Preference Value: " + editTextPreferenceValue);
    }
}

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Preferences Practice</string>
    <string name="mainActivity_button">Show EditText Preference\'s Value</string>
    <string name="preferences_editTextPreference_key">EditTextPreferenceKey</string>
    <string name="preferences_editTextPreference_title">EditTextPreference Title</string>


    <string name="preferences_editTextPreference_defaultValue">EditTextPreference Default Value String</string>

</resources>

我尝试使用您提供的代码创建一个新应用。

我不得不重新创建您的布局文件,我做到了:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="my.package.test.MainActivity">

    <TextView
        android:id="@+id/mainActivity_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_below="@id/mainActivity_textView"
        android:onClick="showCheckBoxPreferencesValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICKME"
        />
</RelativeLayout>

(下次请加进去=)! )

我运行代码看起来像这样:

如您所见,它有效。

我认为您的问题可能是您 运行 您的代码不止一次,但第一次没有正确拼写值键(或其他一些不幸)。 你看,你选择将 false 作为最后一个参数发送给 setDefaultValues,这会强制它只读取默认值一次(应用程序首次启动时)。尝试卸载您的应用并重新安装,或将标志设置为 true。

即:

PreferenceManager.setDefaultValues(this, R.xml.preferences, true);