Android复选框偏好已禁用,即使它已在XML中启用

时间:2015-02-16 10:31:01

标签: android xml checkboxpreference preferencefragment

我目前遇到了在preference.xml中启用了禁用复选框首选项的问题。这是在迁移到Metarial设计之后发生的。我尝试以编程方式启用视图,但它没有帮助。

(我想显示截图,但我的代表还不够高:/)

我的preference.xml

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

    <PreferenceCategory android:title="Network Service" >

        <CheckBoxPreference
            android:enabled="true"
            android:key="@string/communication_option"
            android:selectable="true"
            android:defaultValue="true"
            android:shouldDisableView="false"
            android:summary="Allows down- and upload."
            android:title="Enable server communication" />

        <CheckBoxPreference
            android:disableDependentsState="false"
            android:enabled="true"
            android:dependency="@string/communication_option"
            android:key="@string/wifi_sync_option"
            android:selectable="true"
            android:defaultValue="true"
            android:shouldDisableView="true"
            android:summary="Do not use mobile bandwidth to download"
            android:title="WiFi synchronization only" />

    </PreferenceCategory>
</PreferenceScreen>

my preferenceFragment

public class FragmentPreference extends PreferenceFragment implements
    SharedPreferences.OnSharedPreferenceChangeListener {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // added to make sure it really is enabled
    CheckBoxPreference cbp = (CheckBoxPreference) getPreferenceManager()
            .findPreference(getString(R.string.communication_option));
    cbp.setEnabled(true);
    cbp.setShouldDisableView(false);

}

我正在使用Theme.AppCompat.Light.NoActionBar主题来添加新工具栏。

我注意到的另一件事是,首选项显示为启用它,屏幕方向更改为横向,即使首选项显示为已禁用,仍可以单击/选中/取消选中首选项。

2 个答案:

答案 0 :(得分:2)

经过一些试验和错误后,我发现我刚刚超载了偏好设置。 更纤薄,更清洁的版本看起来像这样:

<PreferenceCategory android:title="Network Service" >

    <CheckBoxPreference
        android:key="@string/communication_option"
        android:defaultValue="true"
        android:summary="Allows down- and upload."
        android:title="Enable server communication" />

    <CheckBoxPreference
        android:dependency="communication_option"
        android:key="@string/wifi_sync_option"
        android:defaultValue="true"
        android:summary="Do not use mobile bandwidth to download"
        android:title="WiFi synchronization only" />

</PreferenceCategory>

public class FragmentPreference extends PreferenceFragment implements
            SharedPreferences.OnSharedPreferenceChangeListener {     

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
}

答案 1 :(得分:0)

您必须在主要活动的onCreate中设置默认值。

PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);
相关问题