繁体   English   中英

Android - 默认暗模式

[英]Android - default dark mode

我想在我的应用暗模式中实现。 默认情况下,我希望它被系统跟踪,所以在主要活动中我放置了:

 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

它工作正常,但如果用户想改变主意并 select 我的应用程序菜单中的某个选项来关闭/打开黑暗模式,活动正在重新启动并且应用程序仍然遵循系统规则。 我该如何改变呢?

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_color_mode) {
        if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        else
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
        return true;
    }

负责您提到的选项的代码位于 onCreate() 中。 允许用户更改模式的机制不在 onCreate() 中

public class MainActivityextends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }
}

当您明确更改暗模式时,Android 会重新创建活动并因此再次调用onCreate

因此,在您更改暗模式后,您不会注意到更改,因为当系统调用onCreate时再次调用AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM

要使其正常工作,您可以将一个值保存到SharedPreference中,该值可以在设置系统暗模式之前在onCreate中检查。

这可以是 boolean,当您想要手动更改暗模式时,您可以切换它。

这是一个示例

public class MainActivityextends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isSystem = prefs.getBoolean("IS_SYSTEM", true);
        if (isSystem) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        }       
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_color_mode) {
            if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            else
                AppCompatDelegate.setDefaultNightMode(
                        AppCompatDelegate.MODE_NIGHT_YES);
                        
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            prefs.edit().putBoolean("IS_SYSTEM", false).apply();
            return true;
        }   
    
}

更新

效果很好,但是当我退出应用程序然后再次启动时,尽管我已经切换了默认系统模式,但它仍然处于活动状态。 有可能在这里使它以这种方式工作吗?

您可以使用另一个SharedPreference boolean 永久保存

public class MainActivityextends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isSystem = prefs.getBoolean("IS_SYSTEM", true);
        boolean isNight = prefs.getBoolean("IS_NIGHT", false);

        if (isSystem) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        } else if (isNight) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }   
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_color_mode) {

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

            if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                prefs.edit().putBoolean("IS_NIGHT", false).apply();
    
            } else {
                AppCompatDelegate.setDefaultNightMode(
                        AppCompatDelegate.MODE_NIGHT_YES);
                prefs.edit().putBoolean("IS_NIGHT", true).apply();
            }

            prefs.edit().putBoolean("IS_SYSTEM", false).apply();
            return true;
        }   
    
}

暂无
暂无

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

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