简体   繁体   中英

Why CheckBox Preference's summary is appearing like it is disabled & why checkBox's value is not changing after clicking it?

I'm developing a Material Design app & I've declared an xml file for Settings.

The SettingsActivity is appearing perfectly on Android version 5.0 & higher but on Android versions below it is appearing like this (as if the summary & checkBox are disabled):

在此处输入图片说明

Also on clicking on checkBox, nothing is happening. It is remaining as it is.

Here's my SettingsActivity.java file's code :

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

    }

}

Here's activity_settings.xml file's code :

<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>

Here's settings.xml file's code :

<?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>

Here's styles.xml file's code :

<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>

As I'm new to Android Development, I have no clue how to make it look good & make it working .

Please let me know.

Thanks in advance.

Remove this line:

android:dependency="prefNotification"

It sais grey this preference out if the CheckBoxPreference with the name preNotification is unchecked, but preNotification is not a CheckBoxPreference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM