简体   繁体   English

带有偏好的 Osmdroid 夜间模式

[英]Osmdroid Night-Mode with Preferences

So I want to make a CheckBoxPreference to turn on the Night-Mode in osmdroid.But the code doesn't to anything.所以我想制作一个 CheckBoxPreference 来打开 osmdroid 中的夜间模式。但代码没有任何作用。 I made the same thing without PreferenceScreen,a normal LinearLayout with a switch, it worked well, but I wanted to use the preference.我在没有 PreferenceScreen 的情况下做了同样的事情,一个带开关的普通 LinearLayout,效果很好,但我想使用偏好。 Does someone know, what's wrong?有人知道,怎么了?


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.allgemein);

        final SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.gruppen_app", Context.MODE_PRIVATE);


        @SuppressLint("ResourceType") final CheckBoxPreference night_mode = (CheckBoxPreference) findPreference(this.getResources()
                .getString(R.id.night_mode_btn));

        night_mode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if(night_mode.isChecked()) {
                    sharedPreferences.edit().putBoolean("isChecked", true).apply();
                    map.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);
                }else{
                    sharedPreferences.edit().putBoolean("isChecked", false).apply();
                }

                return false;
            }
        });




    }
}
@SuppressLint("ResourceType") final CheckBoxPreference night_mode = (CheckBoxPreference) findPreference(this.getResources()
                .getString(R.id.night_mode_btn));

R.id return int, not string. R.id 返回整数,而不是字符串。

Here:这里:

public boolean onPreferenceChange(Preference preference, Object newValue)

you can do this:你可以这样做:

boolean isChecked = Boolean.parseBoolean(String.valueOf(newValue));

and then just:然后只是:

sharedPreferences.edit().putBoolean("isChecked", isChecked).apply();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM