【问题标题】:Android checkboxpreference disabled even though it's enabled in XML即使在 XML 中启用了 Android 复选框首选项,它也已禁用
【发布时间】:2015-02-16 10:31:00
【问题描述】:

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

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

我的偏好.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>

我的偏好片段

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 主题来添加新工具栏。

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

【问题讨论】:

    标签: android xml checkboxpreference preferencefragment


    【解决方案1】:

    经过几次反复试验,我发现我只是重载了首选项。 更苗条、更干净的版本如下所示:

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

    【讨论】:

      【解决方案2】:

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

      PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);
      

      【讨论】:

      • 您可以发表评论,这只是一个建议
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多