![](/img/trans.png)
[英]How do I save and display my data in SharedPreferences as a list? - Java
[英]How do I save the theme of my app in SharedPreferences?
我創建了一個應用程序,我可以在其中自定義主題並使用導航視圖抽屜上的切換按鈕將其從淺色更改為深色。 但是我只能保存 Switch 按鈕的 state 而不能保存 Theme 狀態。 因此,如果有問題或者我可以對代碼做些什么,以便能夠將深色/淺色主題保存到 go 中,請提供幫助,同時切換按鈕檢查狀態。 問題是,當我單擊切換按鈕時一切正常,但是當我關閉應用程序並再次打開它時,切換按鈕仍處於選中狀態,但主題恢復為默認值(淺色主題)。 有人可以幫忙嗎? 我的代碼如下所示:
使用 Class:
public class Utils {
private static int sTheme;
public final static int THEME_DARK = 1;
public final static int THEME_LIGHT = 0;
//Set the theme of the Activity and restart the activity
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
activity.overridePendingTransition(0,0);
}
//Set the theme of the activity according to the configuration
public static void onActivityCreateSetTheme(Activity activity) {
switch (sTheme) {
default:
case THEME_LIGHT:
activity.setTheme(R.style.AimBet_Light);
break;
case THEME_DARK:
activity.setTheme(R.style.AimBet_Dark);
break;
}
}
}
主要活動:
公共 class MainActivity 擴展 AppCompatActivity 實現 NavigationView.OnNavigationItemSelectedListener {
SwitchMaterial themeSwitch;
SharedPreferences myPrefs;
SharedPreferences.Editor myEditor;
private static final String MY_PREFS = "switchPrefs";
private static final String DARK_MODE_SWITCH_STATUS = "switchStatus";
boolean dark_mode_switch_status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_main);
navigationView.getMenu().findItem(R.id.nav_theme).setActionView(new SwitchMaterial(this));
themeSwitch = (SwitchMaterial) navigationView.getMenu().findItem(R.id.nav_theme).getActionView();
myPrefs = getSharedPreferences(MY_PREFS, MODE_PRIVATE);
myEditor = myPrefs.edit();
dark_mode_switch_status = myPrefs.getBoolean(DARK_MODE_SWITCH_STATUS, false); //false is the default value
themeSwitch.setChecked(dark_mode_switch_status);
themeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (themeSwitch.isChecked()) {
Utils.changeToTheme(MainActivity.this, Utils.THEME_DARK);
myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, true);
myEditor.apply();
} else {
Utils.changeToTheme(MainActivity.this, Utils.THEME_LIGHT);
myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, false);
myEditor.apply();
}
}
});
}
}
您沒有在共享首選項中保存任何數據。 像這樣在onCheckedChangedListener
保存它
myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, isChecked)
myEditor.apply()
現在根據保存的主題偏好加載主題。 像這樣更改您的onCreate()
方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPrefs = getSharedPreferences(MY_PREFS, MODE_PRIVATE); // getting shared prefs instance
boolean isDarkTheme = myPrefs.getBoolean(DARK_MODE_SWITCH_STATUS, false); // getting saved theme status
Utils.onActivityCreateSetTheme(this, isDarkTheme); // setting app theme according to the saved theme in shared prefs
setContentView(R.layout.activity_main);
navigationView.getMenu().findItem(R.id.nav_theme).setActionView(new SwitchMaterial(this));
themeSwitch = (SwitchMaterial) navigationView.getMenu().findItem(R.id.nav_theme).getActionView();
myEditor = myPrefs.edit();
dark_mode_switch_status = isDarkTheme;
themeSwitch.setChecked(dark_mode_switch_status);
themeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (themeSwitch.isChecked()) {
Utils.changeToTheme(MainActivity.this, Utils.THEME_DARK);
myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, true);
myEditor.apply();
} else {
Utils.changeToTheme(MainActivity.this, Utils.THEME_LIGHT);
myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, false);
myEditor.apply();
}
}
});
}
現在在您的 Utils class 中,像這樣更改您的onActivityCreateSetTheme
方法。
public static void onActivityCreateSetTheme(Activity activity, boolean isDarkTheme) {
if(isDarkTheme) {
activity.setTheme(R.style.AimBet_Dark);
}else {
activity.setTheme(R.style.AimBet_Light);
}
}
首先,在切換開關時將數據保存到設備中。
myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, isChecked)
myEditor.apply()
然后,在 onCreate()
if(myPrefs.getBoolean(DARK_MODE_SWITCH_STATUS)){
Utils.changeToTheme(MainActivity.this, Utils.THEME_DARK);
} else {
Utils.changeToTheme(MainActivity.this, Utils.THEME_LIGHT);
}
這真的應該工作。 如果還有問題,請告訴我::)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.