简体   繁体   English

如何使用开关在我的应用程序上设置暗模式

[英]How can I set dark Mode on my app with the switch

I know it might be easy but is just something that I don't really know.我知道这可能很容易,但这只是我不知道的事情。 I am new at coding and I tried different codes in my app.我是编码新手,我在我的应用程序中尝试了不同的代码。

The code that I currently used does nothing:我目前使用的代码什么都不做:

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
                    case Configuration.UI_MODE_NIGHT_YES:
                    case Configuration.UI_MODE_NIGHT_NO:

                        break;
                }
            }
        }
    });

I don't know if this is the one that is meant to do its thing or not.我不知道这是否是该做的事情。 I just want a code that should solve my problem.我只想要一个可以解决我的问题的代码。

This is the night theme in my values:这是我价值观中的夜间主题:

<style name="Theme.Diligent" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryVariant">@color/black</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/black</item>
    <item name="colorSecondaryVariant">@color/black</item>
    <item name="colorOnSecondary">@color/white</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
 </style>

Thank you in advance.先感谢您。

To enable night mode use AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);要启用夜间模式,请使用AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); and to disable it use AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);并禁用它使用AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); . .

you can do this as following:你可以这样做:

sw_exclusive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked){ 
if (isChecked){ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } } });

and it's better to save this isChecked in Shared preferences and restart your app and in each activity before call super.onCreate() in onCreate() method just check this flag and do the following:最好在共享首选项中保存此 isChecked 并重新启动您的应用程序,并在 onCreate() 方法中调用 super.onCreate() 之前在每个活动中检查此标志并执行以下操作:

if (isChecked){ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); }

So I did this and nothing happened after I pressed the switch:所以我这样做了,按下开关后什么也没发生:

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); }
        }
    });

Do you guys have any suggestions?你们有什么建议吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM