2014-11-05 148 views
2

我有這樣的代碼:怎樣才能獲得價值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

回答

2

試試這個辦法,希望這將幫助你解決你的問題。

sharedPreferences.getBoolean("notification_settings",false) 

getBoolean():需要兩個參數一個是關鍵,一個是默認值

0
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
boolean isChecked = sharedPreferences.getBoolean(getString(R.string.notification_settings), false); 
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.