android - 使用 Switch 按钮切换 dayNight 主题

标签 android button android-widget android-theme

我在我的布局中实现了一个切换按钮,并希望通过该按钮使用 Android dayNight 主题,dayNight 主题可以正常工作,但问题是每当我 < strong>单击开关它不会立即工作,我必须更改 Activity 然后它才能工作,例如如果我在一个 Activity 中单击开关它在我按下后退按钮并移至其他 Activity 并再次返回该 Activity 之前不会做任何事情,而且当我更改我的 Activity 和恢复时我的开关状态始终设置为默认值,请帮助

I am using Android Studio with dayNight theme

下面是我的切换 Activity

   @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 切换 Activity

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

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

像这样的:

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

注意:第二种方式对我来说不是最好的,但你可以试试。

关于android - 使用 Switch 按钮切换 dayNight 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378151/

相关文章:

android - MultiAutoCompleteTextView 不接受键盘 NEXT

android - 位置管理器不会删除位置更新!

android - android中的自定义tabview

android - Intent.ACTION_GET_CONTENT 打开最近的文件,这给出了错误的 URI

jquery:按钮鼠标按下状态改变

java - 调用函数时按钮不会禁用

java - 按钮未在第一次单击时调用 OnClickListener

android - 调用 onClick 时更改 ImageButton 的图像

java - 我想在 Android 中的 SeekBar OnProgres 更改事件上设置图像的亮度?

java - 尚存的配置更改机制如何工作?