gpt4 book ai didi

java - 如何让我的主题以编程方式转移到其他 Activity 中?

转载 作者:行者123 更新时间:2023-12-02 10:23:04 24 4
gpt4 key购买 nike

我在应用程序的设置页面中创建了一个功能,其中包含一个开关 - 当按下时 - 切换到辅助“夜间”主题。我关注了this tutorial大多数情况下。但是,我不知道如何将这种夜间模式带入我的其他 Activity 中?我尝试在我的主要 Activity 中调用“如果开关已检查”,但显然没有看到该开关。我主要需要知道的是,如何检查另一个 Activity 中的开关状态?这是正确的做法吗?如果我错过了这个问题的其他内容,请告诉我。

//======== 设置页面的代码 ========//

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);


// ======== Night Mode ======== //
SwitchCompat switchCompat;
final SharedPref sharedPref;

sharedPref = new SharedPref(this);

if (sharedPref.loadNightModeState()) {
setTheme(R.style.AppTheme_Night);
getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
} else setTheme(R.style.AppTheme);

setContentView(R.layout.activity_settings);
switchCompat = (SwitchCompat) findViewById(R.id.night_switch);
if (sharedPref.loadNightModeState()) {
switchCompat.setChecked(true);
}

switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
sharedPref.setNightModeState(true);
restartApp();
} else {
sharedPref.setNightModeState(false);
restartApp();
}
}
});
}




private void restartApp() {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
finish();
}

//======== SharedPref 代码 ========//

public class SharedPref {

private SharedPreferences sharedPreferences;


public SharedPref(Context context) {
sharedPreferences = context.getSharedPreferences("filename", Context.MODE_PRIVATE);
}


public void setNightModeState(Boolean state) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("NightMode",state);
editor.apply();
}

public Boolean loadNightModeState (){
Boolean state = sharedPreferences.getBoolean("NightMode", false);
return state;
}

最佳答案

在您的应用程序类中onCreate

SharedPreferences sharedPreferences = getSharedPreferences("Your_Shared_pref", Context.MODE_PRIVATE);
boolean nightMode = sharedPreferences.getBoolean(SettingsActivity.DARK_THEME_PREFERENCE_KEY, false);

AppCompatDelegate.setDefaultNightMode(nightMode ? MODE_NIGHT_YES : MODE_NIGHT_NO);

在您的 Activity 中,执行以下操作:

 switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
AppCompatDelegate.setDefaultNightMode(isChecked ? MODE_NIGHT_YES : MODE_NIGHT_NO);
if (isChecked) {
sharedPref.setNightModeState(true);
recreate();
} else {
sharedPref.setNightModeState(false);
recreate();
}
}
});

@Override
public void recreate() {
finish();
overridePendingTransition(R.anim.anime_fade_in,
R.anim.anime_fade_out);
startActivity(getIntent());
overridePendingTransition(R.anim.anime_fade_in,
R.anim.anime_fade_out);
}

您可以在线找到动画xml。

关于java - 如何让我的主题以编程方式转移到其他 Activity 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com