如果禁用了另一个SwitchPreference,如何禁用SwitchPreference?

时间:2018-12-21 23:02:07

标签: java android xml android-studio

我正在从事游戏的“设置”活动。我有一个通知部分,用户可以在其中启用通知并启用振动。

如果通知SwitchPreference被禁用,我想禁用振动SwitchPreference。我尝试过android:dependancy,但所做的只是模糊SwitchPreference而不是禁用振动。

这是pref_main.xml里面的内容

<PreferenceCategory android:title="@string/pref_title_notifications">
    <SwitchPreference
        android:defaultValue="true"
        android:key="@string/enable_notifications"
        android:title="@string/title_enable_notifications" />

    <RingtonePreference
        android:defaultValue="content://settings/system/notification_sound"
        android:dependency="enable_notifications"
        android:key="@string/key_ringtone"
        android:ringtoneType="notification"
        android:summary="@string/default_ringtone"
        android:title="@string/title_ringtone" />

    <SwitchPreference
        android:defaultValue="true"
        android:key="@string/key_vibrate"
        android:title="@string/title_vibrate" />
</PreferenceCategory>

我有一个设置活动和一个appcompatpreferenceactivity文件。这三个文件控制设置。禁用通知后,如何在每个文件中禁用振动?

编辑:这是MainPreferenceFragment for Settings活动的代码

public static class MainPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_main);

            // EditText Listeners
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_full_name)));
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_username)));
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_birthday)));
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_email)));

            // notification preference change listener
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_ringtone)));

            private SwitchPreferenceCompat enableNotifications;
            private SwitchPreferenceCompat keyRingtone;

            @Override
            public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
                setPreferencesFromResource(R.xml.root_preferences, rootKey);
                enableNotifications = findPreference("enable_notifications");
                keyRingtone = findPreference("key_ringtone");

                enableNotifications.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        keyRingtone.setChecked(((boolean) newValue));
                        return true;
                    }
                });

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以通过捕获第一个Switch上的更改并更改第二个值来实现:

 public static class MainPreferenceFragment extends PreferenceFragment {

        private SwitchPreference enableNotifications;
        private SwitchPreference keyRingtone;

        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_main);

            // EditText Listeners
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_full_name)));
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_username)));
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_birthday)));
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_email)));

            // notification preference change listener
            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_ringtone)));

            enableNotifications = findPreference("enable_notifications");
            keyRingtone = findPreference("key_ringtone");

            enableNotifications.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    keyRingtone.setChecked(((boolean) newValue));
                    return true;
                }
            });

        }