使用“切换"按钮切换DayNight主题 [英] Use Switch button to toggle dayNight theme

查看:116
本文介绍了使用“切换"按钮切换DayNight主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在布局中实现了切换"按钮,并希望通过该按钮使用Android dayNight主题,dayNight主题还可以,但是问题是我什么时候单击不能立即生效的开关,我必须更改活动,然后它才能生效,例如,如果我单击一项活动中的开关,则例如直到我按下后退"按钮并移至其他活动然后再次返回该活动之前,我什么都不会做,并且当我更改活动并恢复时我的开关状态始终设置为默认值,请帮助

I have implemented a Switch button in my layout and want to use Android dayNight theme using the button, dayNight theme works okay but the problem is that when ever I click the switch it does not work instantly, i have to change activities and then it works, for example if I click on switch in one activity it will not do anything until i press the back button and move to some other activity and again come back to that activity and also my switch state always set to default when ever I change my activity and comeback, please help

我正在使用具有dayNight主题的Android Studio

I am using Android Studio with dayNight theme

以下是我的切换活动

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

        setContentView(R.layout.settings_page);
        DayNightt = (Switch)findViewById(R.id.dayNight_Switch);
        DayNightt.setChecked(false);
        DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                }
            }
        });
        super.onCreate(savedInstanceState);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

XML交换活动

<Switch
        android:id="@+id/dayNight_Switch"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="58dp"
        android:checked="false"
        android:text="Switch" />

更改 开关后,

推荐答案

添加此方法:

super.recreate();

重新创建 活动,如果工作正常,则设置正确的主题.

This recreates the activity and if work fine sets the correct theme.

类似的东西:

DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    super.recreate();
                    //Or this
                    recreate();

                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    super.recreate();
                    //Or this
                    recreate();
                }
            }
        });

更新:

如果第一种方法不起作用,则可以尝试以下操作:

If the first method not Works you can try this:

DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    Intent intent = getActivity().getIntent();
                    getActivity().finish();
                    startActivity(intent);

                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    Intent intent = getActivity().getIntent();
                    getActivity().finish();
                    startActivity(intent);
                }
            }
        });

注意:这对我来说不是最好的第二种方式,但是您可以尝试.

note: this second way for me is not the best but you can try.

这篇关于使用“切换"按钮切换DayNight主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆