【问题标题】:Use Switch button to toggle dayNight theme使用切换按钮切换昼夜主题
【发布时间】:2018-01-04 19:09:38
【问题描述】:

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

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

下面是我的 Switch 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 切换活动

<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" />

【问题讨论】:

    标签: android button android-widget android-theme


    【解决方案1】:

    change之后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);
                    }
                }
            });
    

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

    【讨论】:

    • super.recreate() 不适用于我的情况,当我单击切换按钮时重新创建只是冻结应用程序
    • 尝试在activity的super.oncreate()之后添加代码。
    • 不,先生,它仍然冻结:(还有其他选择吗?
    • @RohitB 我用其他可能的方式更新我的答案。
    • sir getActivity() 显示无法解决方法错误
    【解决方案2】:

    我已经通过在我的活动中实现 onPause() 和 onResume() 方法解决了我的切换按钮状态更改,现在切换状态在更改活动后保持不变,我还在我的活动中实现了一个捆绑包。我已经在 onCreate 方法之后添加了它。

    更新代码

     private static Bundle bundle = new Bundle();
    
       @Override
        public void onPause() {
            super.onPause();
            bundle.putBoolean("ToggleButtonState", DayNightt.isChecked());
        }
        @Override
        public void onResume() {
            super.onResume();
            DayNightt.setChecked(bundle.getBoolean("ToggleButtonState",false));
        }
    

    唯一剩下的问题是我必须切换 Activity 才能看到它的实现,我会在某个时候克服它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 2013-01-18
      • 2021-04-26
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多