我已经在布局中实现了“开关”按钮,并希望使用该按钮使用Android dayNight主题,dayNight主题可以正常工作,但是问题是,当我单击开关时,它不能立即生效,我必须更改活动,然后它才能工作,例如,如果我在一个活动中单击“切换”,它将不执行任何操作,直到我按下“后退”按钮并移至其他活动,然后再次回到该活动,并且当我更改活动时,切换状态始终设置为默认状态再回来,请帮忙


  我正在使用带有DayNight主题的Android Studio


以下是我的切换活动

   @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();


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

像这样:

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();
                }
            }
        });


更新:

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

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);
                }
            }
        });


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

10-07 19:41
查看更多