Java 为什么选择复选框首选项';的摘要显示为已禁用&;为什么选中复选框';s值在单击后没有更改?

Java 为什么选择复选框首选项';的摘要显示为已禁用&;为什么选中复选框';s值在单击后没有更改?,java,android,android-xml,android-preferences,android-checkbox,Java,Android,Android Xml,Android Preferences,Android Checkbox,我正在开发一个材料设计应用程序&我已经为设置声明了一个xml文件 SettingsActivity在Android 5.0及更高版本上显示得非常完美,但在以下Android版本上,它的显示方式如下(好像summary&复选框被禁用): 此外,单击复选框时,什么也没有发生。它保持原样 这是我的设置活动.java文件的代码: public class SettingsActivity extends AppCompatActivity { Toolbar toolbar; @O

我正在开发一个材料设计应用程序&我已经为设置声明了一个xml文件

SettingsActivity在Android 5.0及更高版本上显示得非常完美,但在以下Android版本上,它的显示方式如下(好像summary&复选框被禁用):

此外,单击复选框时,什么也没有发生。它保持原样

这是我的
设置活动.java
文件的代码

public class SettingsActivity extends AppCompatActivity {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        SpannableString s = new SpannableString("Settings");
        s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();

    }

    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";

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

            getActivity().setTheme(R.style.prefCategoryStyle);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);

        }

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                              String key) {
            if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
                // do something
            }
        }

    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/windowBackground"
                tools:context="com.abc.xyz.SettingsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_height="match_parent" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:key="prefNotification"
        android:title="@string/prefNotification">
    <CheckBoxPreference
        android:id="@+id/prefNotifyCheckBox"
        android:dependency="prefNotification"
        android:key="prefNotify"
        android:summary="@string/pref_notify_summary"
        android:defaultValue="true"/>
    </PreferenceCategory>
</PreferenceScreen>
<resources>

    <!-- Base application theme.
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
         Customize your theme here.
    </style> -->

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:preferenceStyle">@style/prefCategoryStyle</item>
    </style>

    <style name="prefCategoryStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textColor">@color/navigationBarColor</item>
    </style>

    <style name="MyMaterialTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <style name="MyMaterialTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>
这里是
活动设置.xml
文件的代码

public class SettingsActivity extends AppCompatActivity {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        SpannableString s = new SpannableString("Settings");
        s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();

    }

    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";

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

            getActivity().setTheme(R.style.prefCategoryStyle);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);

        }

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                              String key) {
            if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
                // do something
            }
        }

    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/windowBackground"
                tools:context="com.abc.xyz.SettingsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_height="match_parent" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:key="prefNotification"
        android:title="@string/prefNotification">
    <CheckBoxPreference
        android:id="@+id/prefNotifyCheckBox"
        android:dependency="prefNotification"
        android:key="prefNotify"
        android:summary="@string/pref_notify_summary"
        android:defaultValue="true"/>
    </PreferenceCategory>
</PreferenceScreen>
<resources>

    <!-- Base application theme.
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
         Customize your theme here.
    </style> -->

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:preferenceStyle">@style/prefCategoryStyle</item>
    </style>

    <style name="prefCategoryStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textColor">@color/navigationBarColor</item>
    </style>

    <style name="MyMaterialTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <style name="MyMaterialTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>
这里是
styles.xml
文件的代码

public class SettingsActivity extends AppCompatActivity {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        SpannableString s = new SpannableString("Settings");
        s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();

    }

    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";

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

            getActivity().setTheme(R.style.prefCategoryStyle);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.settings);

        }

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                              String key) {
            if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
                // do something
            }
        }

    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/windowBackground"
                tools:context="com.abc.xyz.SettingsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_height="match_parent" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:key="prefNotification"
        android:title="@string/prefNotification">
    <CheckBoxPreference
        android:id="@+id/prefNotifyCheckBox"
        android:dependency="prefNotification"
        android:key="prefNotify"
        android:summary="@string/pref_notify_summary"
        android:defaultValue="true"/>
    </PreferenceCategory>
</PreferenceScreen>
<resources>

    <!-- Base application theme.
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
         Customize your theme here.
    </style> -->

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:preferenceStyle">@style/prefCategoryStyle</item>
    </style>

    <style name="prefCategoryStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textColor">@color/navigationBarColor</item>
    </style>

    <style name="MyMaterialTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <style name="MyMaterialTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

真的
假的
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@样式/预分类样式
@颜色/导航栏颜色
由于我是Android开发的新手,我不知道如何让它看起来很好并工作起来

请让我知道

提前感谢。

删除此行:

android:dependency="prefNotification"

如果名称为
preNotification
CheckBoxPreference
未选中,但
preNotification
不是
CheckBoxPreference
,则表示此首选项为灰色

要更新复选框值,您必须为实现onClick方法prefNotifyCheckBox@Phoebe我不想捕捉它的值,我只希望它在选中和未选中之间切换。就这样!您可以尝试实现
prefNotifyCheckBox.onClick
方法。在内部尝试设置
prefNotifyCheckBox.setChecked(bool_值)而布尔值为真,并通过versa切换复选框。