如何在我的 android 应用程序中保存夜间模式的状态?
How can I save the state of the night mode in my android app?
我的应用程序中有一个设置-activity,用户可以在其中选择浅色模式、深色模式,并使用三个单选按钮跟随系统。但是当应用程序重新启动时,无论在单选按钮上选择了什么,系统始终会应用。我用 SharedPreferences 试过了,但没有用。我该如何解决这个问题?
Java class:
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:
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)
}
}
在您的设置 Activity 中,您需要这样设置:
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)
希望对您有所帮助!
我的应用程序中有一个设置-activity,用户可以在其中选择浅色模式、深色模式,并使用三个单选按钮跟随系统。但是当应用程序重新启动时,无论在单选按钮上选择了什么,系统始终会应用。我用 SharedPreferences 试过了,但没有用。我该如何解决这个问题?
Java class:
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:
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)
}
}
在您的设置 Activity 中,您需要这样设置:
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)
希望对您有所帮助!