gpt4 book ai didi

java - 如何以编程方式设置第一个和第二个 Activity 的主题?

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:52 26 4
gpt4 key购买 nike

我正在创建具有 2 个 Activity 的应用程序,并希望在第一个和第二个 Activity 中设置主题 onclick 按钮。

方法 setTheme() 仅适用于第一个 Activity

    if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.OLEDTheme);
} else {
setTheme(R.style.AppTheme);
}
Switch oledModeSwitch = (Switch) findViewById(R.id.OLEDSwitch);
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
oledModeSwitch.setChecked(true);
}
oledModeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean Check) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
finish();
if (Build.VERSION.SDK_INT >= 21) {
overridePendingTransition(R.anim.from_alpha, R.anim.to_alpha);
}
startActivity(intent);
if (Check) {
Toast.makeText(getApplicationContext(), "Тема успешно активирована", Toast.LENGTH_SHORT).show();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Toast.makeText(getApplicationContext(), "Тема успешно дезактивирована", Toast.LENGTH_SHORT).show();
}
}
});

主题仅设置第一个 Activity ,但我希望在第一个和第二个 Activity 上设置

最佳答案

当您调用 setTheme() 时,它只会更改发出调用的 Activity 的主题。如果您想更改整个应用程序的主题,则应在 SharedPreferences 中记录主题设置,然后在每个 Activity 中,您应从 SharedPreferences 读取当前主题,并在该 Activity 中调用 setTheme()

关于java - 如何以编程方式设置第一个和第二个 Activity 的主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324753/

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