通过 RadioButton(在 Radio Group 内)通过 sharedpreferences 保存选定的灯光或夜间模式

Save the selected Light or night mode via sharedpreferences via RadioButton (within Radio Group)

如何通过 RadioButton(在 Radio 组内)通过 sharedpreferences 保存选定的灯光或夜间模式?

我了解如何在 sharedpreferences 中保存纯文本。但我不清楚如何通过共享首选项保存夜间和灯光模式。请告诉我们如何通过 sharedpreferences 保存。我想通过 RadioButton(在 RadioGroup 内)保存打印的选项。

谢谢!

首先,您需要在共享首选项中保存布尔类型。

private final SharedPreferences mSharedPreferences;
mSharedPreferences = context.getSharedPreferences("example_name", Context.MODE_PRIVATE);
//SAVE BOOLEAN WITH KEY AND VALUE
mSharedPreferences.edit().putBoolean(key, value).apply();
//GET BOOLEAN WITH KEY AND DEFAULT VALUE, IF IT DOESN'T CONTAIN IT
Boolean myBoolean = mSharedPreferences.getBoolean(key, defaultValue);

之后,您需要在 OnStart 方法中进行设置

AppCompatDelegate.setDefaultNightMode(mSharedPreferences.getDayNightMode());