简体   繁体   English

当应用程序首次启动时,我如何在第一个活动上实现暗模式

[英]How can i implement dark mode on the first activity while the application launched for the first time

I implement dark mode with SwitchPreference in the second activity and in the first activity just a button take me to the second activity (Setting) the problem is when the app destroyed and i launched it again the dark mode not implemented until the second activity launched.我在第二个活动中使用 SwitchPreference 实现暗模式,在第一个活动中只有一个按钮将我带到第二个活动(设置)问题是当应用程序被销毁并且我再次启动它时暗模式直到第二个活动启动才实现。

    //First Activity
    
    public class MainActivity extends AppCompatActivity {
    
        Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
 SharedPreferences sharedPreferences =
                PreferenceManager.getDefaultSharedPreferences( this/* Activity context */);
        Boolean theme = sharedPreferences.getBoolean("darkMode", false);
                
 button=findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent2=new Intent(MainActivity.this,Settings.class);
                    startActivity(intent2);
                }
            });
    
    
    
        }
    
    }

i have some idea that i should use sharedpreferences but i don't know how can i set it我知道我应该使用 sharedpreferences 但我不知道如何设置它

//Second Activity

    public class MySettingsFragment extends PreferenceFragmentCompat {
    
        @Override
        public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
            setPreferencesFromResource(R.xml.preferences,rootKey);
            SwitchPreference switchPreference=findPreference("darkMode");
    
            if (switchPreference.isChecked()){
    
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
            }else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
            }
    
    
        }
    }

在此处输入图像描述

Overview:-概述:-

You can change dark mode or light mode via this function.您可以通过此 function 更改暗模式或亮模式。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.HERE_YOUR_MODE);

You can access the mode like this:您可以像这样访问模式:

AppCompatDelegate.MODE_NIGHT_YES // for dark mode
AppCompatDelegate.MODE_NIGHT_NO // for light mode
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // for set as your devices theme
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // when you are in battery saver

Implementation:-执行:-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean theme = sharedPreferences.getBoolean("darkMode", false);
    if (theme){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); // implement here.
    }else{
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); // implement here.
    }
}

Well as per your code, you are just one step away from achieving result那么根据你的代码,你离取得结果只有一步之遥

you just need to use below code in every activity's onCreate method您只需要在每个活动的onCreate方法中使用以下代码

if (isDarkMode){ // isDarkMode is boolean which we can get from preference
    
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
            }else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
            }
    ```

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

相关问题 如何在 java swing gui 应用程序中实现明暗模式? - How can i implement dark and light mode in a java swing gui application? 该应用程序如何在首次启动时打开一个不同的活动,然后又打开另一个活动? - How to have the app open a different activity for the first time it is launched and a different one from then on? 如何检测用户是否是第一次使用我的应用程序? - How can I detect if a user is using my application for the first time? 活动首次启动时如何停止广播接收器被触发 - How to stop broadcast receiver to be fired when activity first launched NumberFormatingExcepton 如果应用程序是第一次启动 - NumberFormatingExcepton if App is launched First Time 如何在所有活动中实现暗模式? - How do I implement dark mode in all activites? MediaPlayer - 如何在第一次启动应用程序时显示警报对话框 - MediaPlayer - How to show a alert dialoge when app is launched first time 如何在每项活动中使黑暗模式 - How to make dark mode in every activity 如何使用 Java 实现图的广度优先搜索? - How can I implement a Breadth first search of a graph using Java? 我如何防止用户按下“后退”按钮,因为如果用户这样做,它将关闭应用程序并从第一个活动中重新启动它 - How can I prevent a user from pressing the back button, because if the user does it closes the application and restarts it from the first activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM