gpt4 book ai didi

Android v14 SwitchPreference 不更改值

转载 作者:行者123 更新时间:2023-11-29 01:11:16 24 4
gpt4 key购买 nike

类文件

import android.support.v14.preference.SwitchPreference;
//...

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.pref_blank);
this.preferenceScreen = getPreferenceScreen();
}

public void resetDevices(ArrayList<DeviceAdapterItem> items) {
preferenceScreen.removeAll();
preferenceScreen.setPersistent(false);

for(DeviceAdapterItem i : items) {

ExpandablePreferenceCategory category = buildCategory(i);
preferenceScreen.addPreference(category);

Preference deviceTypePreference = buildDeviceTypePreference(i);
deviceTypePreference.setOnPreferenceChangeListener(this);
category.addPreference(deviceTypePreference);

Preference monitoring = buildMonitoringPreference(i);
monitoring.setOnPreferenceChangeListener(this);
category.addPreference(monitoring);

Preference wifiOnly = buildWifiOnlyPreference(i);
wifiOnly.setOnPreferenceChangeListener(this);
category.addPreference(wifiOnly);

Preference delete = buildDeletePreference(i);
delete.setOnPreferenceChangeListener(this);
category.addPreference(delete);

category.handleCollapseExpand();
}
}

private Preference buildMonitoringPreference(DeviceAdapterItem i) {
SwitchPreference monitoring = new SwitchPreference(context.getSupportActionBar().getThemedContext());
monitoring.setPersistent(false);
monitoring.setTitle(getString(R.string.monitoring));
monitoring.setKey("some bogus value" + i.getDeviceId());

monitoring.setChecked(i.isTakeSnapshots());
monitoring.setSummary(getString(i.isTakeSnapshots() ? R.string.enabled : R.string.disabled ));
return monitoring;
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d("pchange", preference.getTitle() + " = " + newValue.toString());
if(preference instanceof SwitchPreference) {
Log.d("pchange", "its a switch!");
((SwitchPreference) preference).setChecked((boolean)newValue);
}
return true;
}

pref_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:persistent="false"
>

</PreferenceScreen>

开关似乎已关闭(当它应该打开时)。当我单击 UI 中的开关时,会触发 onPreferenceChange 并且 newValue 会切换。 UI 永远不会改变,它总是停留在关闭状态?

最佳答案

如果你使用

implementation 'com.android.support:preference-v7:27.1.1'

你应该使用SwitchPreferenceCompat 像这样

<SwitchPreferenceCompat
android:key="taskSendNotification"
android:title="@string/notificationTask"
android:defaultValue="true"></SwitchPreferenceCompat>

android.support.v14.preference.SwitchPreference 不适用于 'com.android.support:preference-v7:27.1.1'

关于Android v14 SwitchPreference 不更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42776568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com