[英]How can I save the state of the night mode in my android app?
我的应用程序中有一个设置活动,用户可以在其中选择亮模式、暗模式,并使用三个单选按钮跟随系统。 但是当应用程序重新启动时,无论在单选按钮上选择什么,系统始终都会应用。 我用 SharedPreferences 尝试过,但没有用。 我怎样才能解决这个问题?
Java类:
package com.example.formelrechner; import... public class Einstellungen extends AppCompatActivity { RadioGroup radioGroup; RadioButton radioButton, radioButton2, radioButton3; @Override protected void onCreate(Bundle savedInstanceState) { if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MO DE_NIGHT_YES) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_NO) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } super.onCreate(savedInstanceState); setContentView(R.layout.activity_einstellungen); radioGroup = findViewById(R.id.radiogroup); radioButton = findViewById(R.id.radioButton); radioButton2 = findViewById(R.id.radioButton2); radioButton3 = findViewById(R.id.radioButton3); radioButton.setChecked(Update("SaveStateOne")); radioButton2.setChecked(Update("SaveStateTwo")); radioButton3.setChecked(Update("SaveStateThree")); radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean one_isChecked) { SaveintosharedPrefs("SaveStateOne", one_isChecked); if (one_isChecked) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } } }); radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean two_isChecked) { SaveintosharedPrefs("SaveStateTwo", two_isChecked); if (two_isChecked) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } } }); radioButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean three_isChecked) { SaveintosharedPrefs("SaveStateThree", three_isChecked); if (three_isChecked) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); } } }); } private void SaveintosharedPrefs(String key, boolean value) { SharedPreferences sp = getSharedPreferences("SaveState",MODE_PRIVATE); final SharedPreferences.Editor editor = sp.edit(); editor.putBoolean(key,value); editor.apply(); } private boolean Update(String key) { SharedPreferences sp = getSharedPreferences("SaveState",MODE_PRIVATE); return sp.getBoolean(key, false); } }
我在 Kotlin 中为我的应用程序做了同样的事情。 但是你可以把它转换成java,几乎是一样的。
您需要为共享首选项创建一个新类:
class SettingsSaveData(context:Context) {
private var sharedPreferences: SharedPreferences =context.getSharedPreferences("file", Context.MODE_PRIVATE)
fun setDarkMode(state: Boolean){
val editor = sharedPreferences.edit()
editor.putBoolean("Dark", state)
editor.apply()
}
fun loadDarkMode(): Boolean? {
val state = sharedPreferences.getBoolean("Dark", false)
return (state)
}
}
在你的设置活动中,你需要有这样的东西:
class SettingsActivity : AppCompatActivity() {
private lateinit var settingSaveData: SettingsSaveData
override fun onCreate(savedInstanceState: Bundle?) {
settingSaveData = SettingsSaveData(this) // make sure to setTheme here
if (settingSaveData.loadDarkMode() == true){ // before onCreate method
setTheme(R.style.DarkTheme)
}else{
setTheme(R.style.AppTheme)
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
if(settingSaveData.loadDarkMode() == true){
switch_theme.isChecked = true // switch_theme is my Switch
}
switch_theme.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
settingSaveData.setDarkMode(true)
restartApplication()
}else{
settingSaveData.setDarkMode(false)
restartApplication()
}
private fun restartApplication(){
val i = Intent(applicationContext, SettingsActivity::class.java)
startActivity(i)
finish()
}
在您的所有活动中,您只需要声明:
private lateinit var settingsSaveData:SettingsSaveData
并在您的 onCreate 方法中设置主题:
settingsSaveData = SettingsSaveData(this)
if (settingsSaveData.loadDarkMode() == true){
setTheme(R.style.DarkTheme)
}else{
setTheme(R.style.AppTheme)
}
super.onCreate(savedInstanceState)
我希望这能帮到您!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.