Enable/disable 启用一个开关首选项时还有两个开关首选项

Enable/disable two more SwitchPreference when one switch Preference is enabled

选择一个首选项后,其他两个首选项应可供点击,否则应 shady/dim 以便用户知道它们不可点击。就像我们如何为文本设置 alpha 一样。 下面附上代码。如果有解决办法请告诉我。

<PreferenceCategory
        android:title="@string/category_dnd_title"
        app:iconSpaceReserved="false">

        <androidx.preference.SwitchPreferenceCompat
            android:defaultValue="false"
            android:key="key1"
            android:title="title1" />

        <androidx.preference.SwitchPreferenceCompat
            android:defaultValue="false"
            android:dependency="key1"
            android:key="key2"
            android:title="title2" />

        <androidx.preference.SwitchPreferenceCompat
            android:defaultValue="false"
            android:dependency="key1"
            android:key="@key3"
            android:title="title3"/>
    </PreferenceCategory>

为此,您需要一些片段 class 来管理代码中的首选项。

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;
            }
        });

    }
}

@编辑
您可以覆盖 SwitchPreferenceCompat class 以产生 enabled/disabled 效果

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");