为此,您需要一些片段类来管理您在代码中的偏好。
public class PreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.your_xml, rootKey);
final SharedPreferences preferences = androidx.preference.PreferenceManager.getDefaultSharedPreferences(getActivity());
final SwitchPreferenceCompat switchPreference1 = (SwitchPreferenceCompat) findPreference("key1");
final SwitchPreferenceCompat switchPreference2 = (SwitchPreferenceCompat) findPreference("key2");
final SwitchPreferenceCompat switchPreference3 = (SwitchPreferenceCompat) findPreference("key3");
if (preferences.getBoolean("key1", false)) {
switchPreference2.setEnabled(true);
switchPreference3.setEnabled(true);
} else {
switchPreference2.setEnabled(false);
switchPreference3.setEnabled(false);
}
switchPreference1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean turned = (Boolean) newValue;
if (turned) {
switchPreference2.setEnabled(true);
switchPreference3.setEnabled(true);
} else {
switchPreference2.setEnabled(false);
switchPreference3.setEnabled(false);
}
preferences.edit().putBoolean("key1", turned).apply();
return true;
}
});
}
}
@EDIT
您可以覆盖 SwitchPreferenceCompat 类以启用/禁用效果
public class CustomSwitchPreference extends SwitchPreferenceCompat {
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle, int defStyleRes) {
super(context, attrs, defStyle, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context, null);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView title = (TextView) holder.findViewById(android.R.id.title);
TextView summary = (TextView) holder.findViewById(android.R.id.summary);
if (title.isEnabled()) {
title.setTextColor(Color.BLACK);
} else {
title.setTextColor(Color.GRAY);
}
if (summary.isEnabled()) {
summary.setTextColor(Color.BLACK);
} else {
summary.setTextColor(Color.GRAY);
}
}
}
在这种情况下,您的偏好 xml 应如下所示
<PreferenceCategory
android:title="Category"
app:iconSpaceReserved="false"
>
<com.test.CustomSwitchPreference
android:defaultValue="false"
android:key="key1"
android:title="title1"
/>
<com.test.CustomSwitchPreference
android:defaultValue="false"
android:key="key2"
android:title="title2"
/>
<com.test.CustomSwitchPreference
android:defaultValue="false"
android:key="key3"
android:title="title3"
/>
</PreferenceCategory>
而且您应该更改对片段中首选项的访问,例如
final CustomSwitchPreference switchPreference1 = (CustomSwitchPreference) findPreference("key1");
final CustomSwitchPreference switchPreference2 = (CustomSwitchPreference) findPreference("key2");
final CustomSwitchPreference switchPreference3 = (CustomSwitchPreference) findPreference("key3");