如何使用switchCompat

时间:2019-04-26 09:45:32

标签: android sharedpreferences android-theme

我想使用SwithCompat动态更改应用程序的主题,所以我实现了这一点:

public class SettingsActivity extends AppCompatActivity {

    private final static int THEME_LIGHT = 2;
    private final static int THEME_DARK = 1;

    @BindView(R.id.summary)
    TextView summary;
    @BindView(R.id.themeModeSwitchCompat)
    SwitchCompat themeMode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        SharedPreferences sharedPreferences = getSharedPreferences("VALUES", MODE_PRIVATE);
        int theme = sharedPreferences.getInt("THEME", 2);
        switch (theme) {
            case 1:
                setTheme(R.style.CustomStyle_DarkTheme);
                break;
            case 2:
                setTheme(R.style.CustomStyle_LightTheme);
                break;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        ButterKnife.bind(this);

        themeMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                summary.setText(getResources().getString(R.string.night_mode_on_summary));
                sharedPreferences.edit().putInt("THEME",1).apply();
            } else {
                summary.setText(getResources().getString(R.string.night_mode_off_summary));
                sharedPreferences.edit().putInt("THEME",2).apply();
            }
        });

    }}

我想知道为什么主题不会更改,以及如何更正我的代码以获取使用switchCompat动态更改的主题

2 个答案:

答案 0 :(得分:0)

在xml中创建两个不同的主题。您甚至可以创建两个不同的xml文件。 就像RedTheme.xml和GreenTheme.xml一样。创建样式时,不要忘记将父级设置为“ AppTheme”。

import pulsar
import re
client = pulsar.Client('pulsar://localhost:6650')
topic='my-topic'
topic=['topic-1', 'topic-2', 'topic-3']
topic=re.compile('topic-.*')
print(topic)
<_sre.SRE_Pattern object at 0x7f13314e7210> 
consumer = client.subscribe(topic, "my-subscription")
2019-04-26 07:05:02.956 INFO  ConnectionPool:72 | Created connection for 
pulsar://localhost:6650
2019-04-26 07:05:02.957 INFO  ClientConnection:300 | [127.0.0.1:55874 -> 
127.0.0.1:6650] Connected to broker

现在在setOnCheckedChangeListener上调用setTheme()方法,该方法是在运行时更改主题的本机方法。

<resources>

<style name="RedTheme" parent="AppTheme">
    <item name="colorPrimary">@color/colorRed</item>
    <item name="colorPrimaryDark">@color/colorGreen</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="toolbarNavigationButtonStyle">@style/Toolbar.Button.Navigation.REd</item>
</style>

重要:在您的基础活动中创建此方法。应用主题后,您必须重新创建活动以反映更改。

答案 1 :(得分:0)

尝试一下:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  initViews()
}

private void initViews() {


    SharedPreferences sharedPreferences = getSharedPreferences("VALUES", MODE_PRIVATE);
    int theme = sharedPreferences.getInt("THEME", 2);
    switch (theme) {
        case 1:
            setTheme(R.style.CustomStyle_DarkTheme);
            break;
        case 2:
            setTheme(R.style.CustomStyle_LightTheme);
            break;
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    ButterKnife.bind(this);
    themeMode.setOnCheckedChangeListener(null);
    switch (theme) {
        case 1:
            themeMode.setChecked(true);
            break;
        case 2:
            themeMode.setChecked(false);
            break;
    }
    themeMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if (isChecked) {
            summary.setText(getResources().getString(R.string.night_mode_on_summary));
            sharedPreferences.edit().putInt("THEME", 1).apply();
            //Call the onCreate here again to apply the theme again.
            initViews();
        } else {
            summary.setText(getResources().getString(R.string.night_mode_off_summary));
            sharedPreferences.edit().putInt("THEME", 2).apply();
            //Call the onCreate here again to apply the theme again.
            initViews();
        }

    });

}