2013-05-01 62 views
0

我想每次點擊按鈕「保存」時保存用戶使用sharedPreferences選擇的主題,但有一些我明顯做錯了,但我看不到它。SharedPreferences做奇怪的事

我的問題是,沒有sharedPreferences東西我的應用程序更改主題的方式我想要的方式,沒有問題。但是,當我應用sharedPreferences的東西時,它會停止更改主題,而且它也不會保存。

我提出了一些意見,以幫助理解我想要完成什麼和在哪裏。

這裏是我的設置類:

public class SettingsActivity extends Activity implements OnClickListener { 

public static final String PREF_NAME = "MyPrefsFile"; 
public static int newTheme; 
public final static int THEME_DARK = R.style.DarkTheme; 
public final static int THEME_LIGHT = R.style.LightTheme; 
public final static int THEME_COLORS = R.style.ColorsTheme; 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // Here is where I'm supposed to check the sharedPreferences then 
    // Set the theme option newTheme with the users last choice, 
      // and if there is 
    // a choice set the theme. 
    SharedPreferences settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
    newTheme = settings.getInt("themeCustom", 0); 

    if(newTheme == THEME_DARK) { 
     setTheme(R.style.DarkTheme); 

    } else if(newTheme == THEME_LIGHT){ 
     setTheme(R.style.LightTheme); 
    } else if(newTheme == THEME_COLORS) { 
     setTheme(R.style.ColorsTheme); 
    } else { 
    // Utils.onActivityCreateSetTheme(this); 
     setTheme(R.style.AppTheme); 
    } 
    setContentView(R.layout.activity_settings); 

    findViewById(R.id.button2).setOnClickListener(this); 
    findViewById(R.id.button3).setOnClickListener(this); 
    findViewById(R.id.button4).setOnClickListener(this); 
    findViewById(R.id.button5).setOnClickListener(this); 
    findViewById(R.id.button6).setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
    Intent intent = getIntent(); 
    Intent main = new Intent(SettingsActivity.this,MainActivity.class); 
    switch (v.getId()) { 
    case R.id.button2: 
     newTheme = THEME_DARK; 
     finish(); 
     startActivity(intent); 
     break; 

    case R.id.button3: 
     newTheme = THEME_LIGHT; 
     finish(); 
     startActivity(intent); 
     break; 
    case R.id.button5: 
     newTheme = THEME_COLORS; 
     finish(); 
     startActivity(intent); 
     break; 

    case R.id.button4: 
     // This button returns to the main activity without saving. 
     startActivity(main); 
     break; 
    case R.id.button6: 
     // this is the button save 
     SharedPreferences settings = 
           getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
     SharedPreferences.Editor edit; 
     edit = settings.edit(); 
     edit.clear(); 
     edit.putInt("themeCustom", newTheme); 
     edit.commit(); 
     startActivity(main); 
     break; 

    default: 
     break; 
     } 

    } 

    } 
+0

你在MainActivity設置主題? – 2013-05-01 05:51:39

回答

1

我認爲你沒有保存更改。每當你點擊一個主題更改按鈕,你只是分配一個新的主題值到newTheme標籤,然後你finish()該活動。但是在完成活動之前要保存更改的位置?

您應該在完成活動之前提交新的更改,然後我認爲它會反映更改。

+0

這就是發生了什麼!在閱讀您的文章之前,我看到了!還是要給你答案的功勞!非常感謝! – 2013-05-01 05:59:45

1

我想這是因爲你通話清晰putInt在一個SharedPreferencec.Editor交易。 http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()

別叫清楚,只有putInt

+0

改變了它......仍然是同樣的事情。 – 2013-05-01 05:46:23

+0

你在MainActivity中有什麼代碼來初始化主題? – httpdispatch 2013-05-01 05:55:40

+0

Got it!在mainActivity中初始化我使用了我在設置中使用的相同代碼。 – 2013-05-01 06:00:18

0

問題是與int和長在這裏。

這裏newTheme是int,它存儲THEME_DARK或THEME_ILGHT等等。但THEME_DARK和其他變量具有很長的值,即R.style.DarkTheme可以是長或十六進制值。

因此最終當您將這些newTheme保存在sharedPref中時,您實際上只能在此保存0個值。

在寫入Shared Pref之前,您可以檢查newTheme變量的值。這將清除你的問題。

要解決這個問題,您可以使用這些主題的任何其他最終整數值,並讓它們保存到共享的首選項中。

希望這會解決您的問題。

+0

非常感謝!你能向我解釋一點好嗎?我是編程新手。 – 2013-05-01 05:55:59

+0

1.)只需在將newTheme變量的值保存到共享的前綴之前記錄它的值即可。 2.)改爲保存Theme_Dark = R.style.Theme_Dark您必須使用Theme_Dark = 1,Theme_Light = 2;和所有由android提供的Id類似,都是Long值,並且在您的代碼中,您將以整數形式分配這些long(long數據類型)值。這就是爲什麼每次你不知不覺地爲新創建的主題保存O – 2013-05-01 05:57:34

+0

嗯,我會改變它!但現在正在處理@Vipul發佈的答案!但我可能會改變它,以避免將來出現問題。 非常感謝! – 2013-05-01 06:04:22

0

要保存共享偏好,可以使用下面的代碼:

SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); 
SharedPreferences.Editor prefEditor = prefs.edit(); 
prefEditor.putInt(「themeCustom」, 0); 
prefEditor.commit();