安卓系统 - 默认暗黑模式

3

我希望在我的应用程序中实现暗黑模式。默认情况下,我希望它跟随系统,因此在主活动中我放置了以下代码:

 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);
        return true;
    }

请分享一下你在什么时候设置AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM,是在onCreate()方法中吗?请回复并@Zain以便通知。 - undefined
@Zain,感谢你的承诺。你提到的选项所对应的代码位于onCreate()方法中。允许用户更改模式的机制不在onCreate()方法中。 - undefined
2个回答

1

负责您提到的选项的代码位于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 中进行检查。
这可以是一个布尔值,您可以在手动更改暗模式时切换它。
以下是一个示例:
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布尔值来永久保存。

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;
        }   
    
}

1
谢谢,这个很完美,但是当我退出应用程序然后再次启动时,默认的系统模式仍然是激活的,尽管我已经切换了它。在这里有可能让它按照我想要的方式工作吗? - undefined
1
@amtrax 谢谢,请检查更新的答案。 - undefined

-2
尝试这个选项:

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


        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO){
            isNightModeOn = false; // Set Text
        }
        else if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            isNightModeOn = true; // Set Text
        }
        else {AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        };

这是一个默认的深色背景设置。然后你可以按照自己的方式进行切换。

通常只有代码答案会被删除。请解释一下你的解决方案。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接