2011-12-26 56 views
0

我爲我的應用實施了一些應用偏好設置。對於字符串他們工作很好,但不是布爾值。例如。Android應用偏好不起作用

public class MdSharedPrefs { 
    public final static String PREFS_NAME = "prefs"; 

    public static boolean getSSFlag(Context context) { 
     SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 

     return prefs.getBoolean(context.getString(R.string.pref_key_ss), false); 
    } 

    public static void setSSFlag(Context context, boolean newValue) { 
     SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 
     Editor prefsEditor = prefs.edit(); 
     prefsEditor.putBoolean(context.getString(R.string.pref_key_ss), 
       newValue); 
     prefsEditor.commit(); 
    } 

} 

而這就需要

<string name="pref_key_ss"></string> 

中的strings.xml。當我刪除這行時,該項目不會編譯。但是當我離開它時,這個布爾值pref不起作用。當我點擊它並返回到主屏幕,然後返回到首選項時,該值不會被檢查。

不過,對於字符串它工作正常。

任何想法什麼是錯?

在我的應用
+0

我不知道爲什麼」你創建靜態字符串,我th墨水問題與.. – 2011-12-26 13:18:32

+0

ru給空。嘗試使用 anyname 2011-12-26 14:01:20

回答

2

我使用這樣它的正常工作

聲明

private static final String SUNDAY_ON = "sunday_selected"; 

越來越

state = prefs.getBoolean(SUNDAY_ON, false); 

存儲這樣

editor.putBoolean(SUNDAY_ON, value);