【问题标题】:How can I correctly to develop the dark mode in my Android studio?如何在我的 Android 工作室中正确开发暗模式?
【发布时间】:2021-05-31 17:38:30
【问题描述】:

我试图部署一个切换按钮来将布局切换到浅色模式到深色模式,但我在这里遇到了一些问题。当我启动应用程序时,它不能自动切换到黑暗,我之前关闭应用程序之前已经切换到黑暗模式。当我回到设置页面时,它会切换到暗模式,但在这里我得到了错误。应用进入循环,

D/TagLifecycle: In the setting darkModeOff event
D/TagLifecycle: In the activity_main onDestroy() event
D/TagLifecycle: In the activity_main onCreate() event
D/TagLifecycle: In the activity_main onStart() event
D/TagLifecycle: In the activity_main onResume() event
D/TagLifecycle: In the activity_main onPause() event
D/TagLifecycle: In the activity_main onStop() event
D/TagLifecycle: In the activity_user_info onCreate() event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOn event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOff event
D/TagLifecycle: In the activity_main onDestroy() event
D/TagLifecycle: In the activity_main onCreate() event
D/TagLifecycle: In the activity_main onStart() event
D/TagLifecycle: In the activity_main onResume() event
D/TagLifecycle: In the activity_main onPause() event
D/TagLifecycle: In the activity_main onStop() event
D/TagLifecycle: In the activity_user_info onCreate() event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOn event
D/TagLifecycle: In the activity_setting onCreate() event
D/TagLifecycle: In the setting darkModeOff event

这是我的设置页面代码!

private Switch darkSwitch;

public static final String MyPREFERENCES= "nightModePrefs";
public static final String KEY_ISNIGHTMODE = "isNightMode";
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);
    Log.d(TAG_LIFECYCLE, "In the activity_setting onCreate() event");

    fbAuth = FirebaseAuth.getInstance();
    fbStore = FirebaseFirestore.getInstance();
    userID = fbAuth.getCurrentUser().getUid();

    sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    darkSwitch = findViewById(R.id.swDark);

    checkNightModeActivated();

    darkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                saveNightModeState(true);
                recreate();
                Log.d(TAG_LIFECYCLE, "In the setting darkModeOn event");
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                saveNightModeState(false);
                recreate();
                Log.d(TAG_LIFECYCLE, "In the setting darkModeOff event");
            }
        }

    });

    BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);

    bottomNavigationView.setSelectedItemId(R.id.settingActivity);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.mainActivity:
                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                    overridePendingTransition(0,0);
                    return true;
                case R.id.mapActivity:
                    startActivity(new Intent(getApplicationContext(),MapActivity.class));
                    overridePendingTransition(0,0);
                    return true;
                case R.id.userInfoActivity:
                    startActivity(new Intent(getApplicationContext(),UserInfoActivity.class));
                    overridePendingTransition(0,0);
                    return true;
                case R.id.settingActivity:
                    return true;
            }
            return false;
        }
    });
}
private void saveNightModeState(boolean nightMode) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(KEY_ISNIGHTMODE, nightMode);
    editor.apply();
}
public void checkNightModeActivated(){
    if (sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){
        darkSwitch.setChecked(true);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        darkSwitch.setChecked(false);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }
}

【问题讨论】:

    标签: java android firebase sharedpreferences


    【解决方案1】:

    这里有很多问题,您可以改进它。我可以告诉你它循环的原因。

    这就是问题所在:

    当你第一次启动时,你正在使用这个函数调用checkNightModeActivated();检查夜间模式

    函数内部

    public void checkNightModeActivated(){
        if (sharedPreferences.getBoolean(KEY_ISNIGHTMODE, false)){
            darkSwitch.setChecked(true);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            darkSwitch.setChecked(false);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }
    }
    

    您正在标记darkSwitch.setChecked(true);,并且您还为此添加了一个 onCheckedListener,当您在此处将其标记为 true 时会触发它。

    onCheckedListener里面如下:

    darkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    saveNightModeState(true);
                    recreate();
                    Log.d(TAG_LIFECYCLE, "In the setting darkModeOn event");
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    saveNightModeState(false);
                    recreate();
                    Log.d(TAG_LIFECYCLE, "In the setting darkModeOff event");
                }
            }
    
        });
    

    您再次重新创建了整个活动,从那里再次调用 checkNightModeActivated(); 并且我们被循环。

    我会为您推荐的解决方案是将最初检查和应用暗模式的逻辑转移到您自己的个人课程中,该课程扩展了Application() 课程。在它下面,您可以通过 SharedPreference 放置相同的检查逻辑,因为即使您重新创建活动,Application() 类仍然保持完整,您不会被卡住。请注意,您还需要在 Manifest 中注册所述类。

    第二个建议是使用某种按钮来确认更改,而不是直接更改复选框。

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2021-12-23
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多