我正在开发一个在DarkMode主题中使用的应用程序,该应用程序将Switch
启用和禁用darkMode,然后将其保存在布尔型小节中,这意味着darkMode是true还是false(boolean isDarkMode =false;
)
它运作良好,我使用SharedPreferences
保存此true或false值以在其他活动中使用它。
它工作,但是...
当我关闭应用程序并再次运行应用程序时,SharedPreferences的值未保存,并返回到boolian的第一个值
(感谢你)
SettingActivity.java(将布尔型设置为true或false)
package com.kurdfoxx.nightmodewithsharedprefrense;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class SettingActivity extends AppCompatActivity {
Switch mySwitch;
boolean isDarkMode =false;
private static final String DARKMODE = "login";
@Override
protected void onCreate(Bundle savedInstanceState) {
myboolian();
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.darkTheme);
isDarkMode =true;
} else {
setTheme(R.style.AppTheme);
isDarkMode =false;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Toast.makeText(this, ""+ isDarkMode, Toast.LENGTH_SHORT).show();
mySwitch = findViewById(R.id.switch1);
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
mySwitch.setChecked(true);
}
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
isDarkMode =true;
restartApp();
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
isDarkMode =false;
restartApp();
}
}
});
Button buttonGoMain=findViewById(R.id.btn_go_MainAct);
buttonGoMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SettingActivity.this,MainActivity.class));
}
});
}
public void restartApp() {
Intent intentRestartApp = new Intent(getApplicationContext(), SettingActivity.class);
startActivity(intentRestartApp);
finish();
}
public void myboolian(){
isDarkMode =!isDarkMode; //change isDarkMode from false to true by this
SharedPreferences sharedPreferences =getSharedPreferences(DARKMODE,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("ISDARKMODE", isDarkMode);
editor.apply();
}
}
和MainActivity.java
package com.kurdfoxx.nightmodewithsharedprefrense;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String DARKMODE = "login";
boolean isDarkMode = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
getData();
if (isDarkMode){
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.darkTheme);
isDarkMode =true;
} else {
setTheme(R.style.AppTheme);
isDarkMode =false;
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button_go_to_setting_activity);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SettingActivity.class));
}
});
}
private void getData() {
SharedPreferences sharedPreferences = getSharedPreferences(DARKMODE, MODE_PRIVATE);
isDarkMode = sharedPreferences.getBoolean("ISDARKMODE", false);
}
}
最佳答案
在checkedChangedListener中,将darkMode分别设置为true
和false
。
并且在您的myboolian
方法中,您是唯一写入变量的地方。
仅从您的onCreate
调用此方法,而在其他任何地方都不会调用。除此之外,此方法始终设置darkMode = !darkMode
,交换值。
您必须在onCheckedChanged中调用myboolian
并删除darkMode = !darkMode
,否则您将始终重置为该值。