如何获得PreferenceFragment的价值

时间:2014-11-05 09:46:23

标签: java android android-studio sharedpreferences

我有这段代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:key="notification_settings"
        android:text="@string/impostazione"
        android:id="@+id/save_check"
        android:summary="@string/notification_title"
        android:defaultValue="false"

        ></CheckBoxPreference>

</PreferenceScreen>

现在我需要在FragmentA中获取Checkbox值:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

现在?

3 个答案:

答案 0 :(得分:2)

请尝试这种方式,希望这有助于您解决问题。

sharedPreferences.getBoolean("notification_settings",false)

getBoolean():需要两个参数一个是键,另一个是默认值

答案 1 :(得分:0)

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean isChecked = sharedPreferences.getBoolean(getString(R.string.notification_settings), false);

答案 2 :(得分:0)

strings.xml
<string name="notification_setting">notification_settings</string>
---------------------------------
xxx.xml
.....
<CheckBoxPreference android:key="@string/notification_setting"
....
--------------------------------
.java
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean wifiState = sharedPreferences.getBoolean(R.string.setting_wifi_key, false);

wifiState is you value.