Switch 在用户输入后设置的 OnCheckedChangeListener 循环。

huangapple go评论90阅读模式
英文:

Switch setOnCheckedChangeListener looping after user input

问题

以下是翻译好的内容:

我正在尝试立即切换到深色模式(无需重新加载),使用一个片段中的开关。

我用来执行此操作的代码片段如下:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

这在测试中是有效的,但我希望能够使用开关来切换到深色模式,所以我的代码如下:

switch_us_dark_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // 开关已启用
            Log.i("tag", "切换到深色模式");
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            editor.putString("theme", "dark");
        }

        if (!isChecked) {
            // 开关已禁用
            Log.i("tag", "切换到浅色模式");
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            editor.putString("theme", "light");
        }
    }
});

然而,在我首次单击开关后,它会出现循环。

我尝试在一个线程中执行它,但是它报错说必须在主线程上运行。

我将移动开关到按钮下面,以便它看起来不像是表单的一部分,但这不是重点。

提前谢谢!

英文:

I am trying to switch to dark mode immediately(without reloading) using a switch in a fragment.

The piece of code that I am using to do this is

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

This is working in testing but I want it to change to dark mode using the switch so my code is the following.

switch_us_dark_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // The toggle is enabled
                    Log.i("tag", "Hit Dark");
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    editor.putString("theme", "dark");
                }

                if(!isChecked){
                    // The toggle is disable
                    Log.i("tag", "Hit Light");
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    editor.putString("theme", "light");
                }
            }
        });

This is however looping after the first time I click on the switch.
Switch 在用户输入后设置的 OnCheckedChangeListener 循环。

I tried doing it on a thread but it threw me an error saying it must be run on the main thread.

I will move the switch below the button so that it doesnt look like its part of the form but thats besides the point.

Switch 在用户输入后设置的 OnCheckedChangeListener 循环。

Thanks in advance!

答案1

得分: 1

使用以下代码:

switch_us_dark_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.isPressed()) {
            // 开启了切换
            Log.i("tag", "切换为暗黑模式");
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            editor.putString("theme", "dark");
        } else {
            // 关闭了切换
            Log.i("tag", "切换为明亮模式");
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            editor.putString("theme", "light");
        }
    }
});

使用 "buttonView.isPressed()" 替代 isChecked。希望它可以工作。

如果以上方法失败,您还可以使用 Checkbox,并将其设置为可绘制,但仅在以上方法不起作用时使用。

英文:

Use below code:

switch_us_dark_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    // The toggle is enabled
                    Log.i("tag", "Hit Dark");
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    editor.putString("theme", "dark");
                } else {
                    // The toggle is disable
                    Log.i("tag", "Hit Light");
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    editor.putString("theme", "light");
                }
            }
        });

Use "buttonView.isPressed()" instead of isChecked. Hope it works

You can also use the Checkbox instead of Switch and set the drawable to it, but only if above fails.

huangapple
  • 本文由 发表于 2020年8月4日 19:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63245947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定