怎样才能获得价值PreferenceFragment

问题描述:

我有这样的代码:怎样才能获得价值PreferenceFragment

<?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得到复选框值:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 

现在?

+0

试试这个方法:sharedPreferences.getBoolean( “notification_settings”,假) – 2014-11-05 09:59:40

+0

我有默认值,所以我再次设置为false? – user1608228 2014-11-05 10:25:04

+0

getBoolean()是必需的两个参数一个是关键另一个是默认值。 – 2014-11-05 10:28:21

试试这个办法,希望这将帮助你解决你的问题。

sharedPreferences.getBoolean("notification_settings",false) 

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

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

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.