【问题标题】:How do I save the theme of my app in SharedPreferences?如何在 SharedPreferences 中保存我的应用程序主题?
【发布时间】:2021-05-31 13:53:18
【问题描述】:

我创建了一个应用程序,我可以在其中自定义主题并使用导航视图抽屉上的切换按钮将其从浅色更改为深色。但是我只能保存 Switch 按钮的状态,但不能保存 Theme 状态。 因此,如果有问题或者我可以对代码做些什么,以便能够保存深色/浅色主题与“切换”按钮检查状态同时进行,请提供帮助。问题是,当我单击切换按钮时一切正常,但是当我关闭应用程序并再次打开它时,切换按钮仍处于选中状态,但主题恢复为默认值(浅色主题)。有人可以帮忙吗?我的代码如下所示:

实用程序类:

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;
    }
}

}

主要活动:

公共类 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();
            }
        }
    });

}

}

【问题讨论】:

    标签: java android sharedpreferences android-dark-theme


    【解决方案1】:

    首先,切换开关时将数据保存到设备中。

    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);
    }
    

    这确实应该有效。让我知道是否还有问题! :)

    【讨论】:

    • 那也没用。该应用程序只是停留在启动活动中,不会打开任何其他内容。
    【解决方案2】:

    您没有在共享首选项中保存任何数据。像这样保存在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 类中,像这样更改您的 onActivityCreateSetTheme 方法。

    public static void onActivityCreateSetTheme(Activity activity, boolean isDarkTheme) {
        if(isDarkTheme) {
            activity.setTheme(R.style.AimBet_Dark);
        }else {
            activity.setTheme(R.style.AimBet_Light);
        }
    }
    

    【讨论】:

    • 我已经尝试了你的建议,但它不起作用。我还编辑了代码,所以如果有问题或者我可以对代码做些什么来保存深色/浅色主题以与“切换”按钮检查状态齐头并进,请提供帮助。问题是,当我单击切换按钮时一切正常,但是当我关闭应用程序并再次打开它时,切换按钮仍处于选中状态,但主题恢复为默认值(浅色主题)。
    • 您不会在应用重启或应用打开时更改主题,我也在更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多