简体   繁体   English

使用复选框更改应用程序主题(使用Java)

[英]Changing app theme with checkbox (with java)

I'm adding a Dark Mode to my app and I've created a checkbox in the 3-dot menu (toolbar). 我向应用程序添加了暗模式,并在3点菜单(工具栏)中创建了一个复选框。

I want to make the app change the theme to Dark when the checkbox is checked, and revert it back to the Main theme when unchecked. 我想使应用程序在选中复选框时将主题更改为“暗”,并在未选中时将其还原为“主主题”。

Here is my current code for onClick of Dark Mode Checkbox button: 这是我当前的onClick暗模式复选框按钮的代码:

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

            switch (item.getItemId()) {
                case R.id.dark_mode:
                    if (item.isChecked()) {
// If item already checked then unchecked it
                        item.setChecked(false);
                    } else {
// If item is unchecked then checked it
                        item.setChecked(true);
                    }
                default:
                    return super.onOptionsItemSelected(item);
            }
        }

How can I do this using java? 如何使用Java做到这一点?

Also I would like to let the app remember whether the user lastly selected dark mode or not. 我也想让应用程序记住用户是否最后选择了暗模式。 (This is actually required because after the activity is restarted, the app would go back to it's old state.) (这实际上是必需的,因为在活动重新启动后,应用程序将返回到其旧状态。)

To change the theme of your app programatically, you can use setTheme(...) in your onCreate() method right before super.onCreate() (code from this question ): 要以编程方式更改应用程序的主题,可以在super.onCreate()之前super.onCreate() 此问题的代码setTheme(...)onCreate()方法中使用setTheme(...) ):

public void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme); // THIS IS WHERE THE THEME IS SET -- must go before super.onCreate()
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}

However, this won't change the theme once the app is already going. 但是,一旦应用程序运行完毕,这将不会更改主题。 To do that, you will need to either restart the app or just change the backgrounds of your activities without actually changing the theme. 为此,您将需要重新启动应用程序或仅更改活动背景,而无需实际更改主题。

Restarting the app: 重新启动应用程序:

Code comes from this question , answer by Marc: 代码来自这个问题 ,马克回答:

Intent i = getBaseContext().getPackageManager()
             .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

Changing the backgrounds: 更改背景:

If you need to change the background while the app is still running, you can set the background color of a layout like so (here, it's being set to red): 如果需要在应用程序仍在运行时更改背景,则可以像这样设置布局的背景色(在这里,它被设置为红色):

layout.setBackgroundColor(Color.RED);

To save the user's choice after the app closes: 要在应用关闭后保存用户的选择,请执行以下操作:

The best way to do this is to use the SharedPreferences API. 最好的方法是使用SharedPreferences API。 If you want to save the background choice as a String-to-boolean key-value pair, for example (where the key would be the String "background_is_dark" and the value would be either true or false ) you could write this: 例如,如果要将背景选择另存为“字符串到布尔值”的键值对(其中键为String "background_is_dark" ,而值则为truefalse ),则可以这样编写:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("background_is_dark"), true); // here is where you would enter true or false
editor.commit();

To later access that boolean value (like in onCreate() when you need to decide which background to set) you would use this code: 若要稍后访问该布尔值(例如,当需要确定要设置的背景时,就像在onCreate()一样),可以使用以下代码:

Context context = getActivity();
SharedPreferences isDark = context.getSharedPreferences(
        "background_is_dark", Context.MODE_PRIVATE);

See Android's documentation for more information on the SharedPreferences API. 有关SharedPreferences API的更多信息,请参见Android文档

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

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