繁体   English   中英

为什么CheckBox Preference的摘要显示为禁用状态,为什么单击它后CheckBox的值没有变化?

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

我正在开发Material Design应用,并且已经为设置声明了一个xml文件。

在Android 5.0及更高版本上,SettingsActivity可以完美显示,但在以下Android版本上,它看起来像是这样(就像摘要和复选框已禁用):

在此处输入图片说明

同样在单击复选框时,没有任何反应。 它保持原样。

这是我的SettingsActivity.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
            }
        }

    }

}

这是activity_settings.xml文件的代码

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

这是settings.xml文件的代码

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

这是styles.xml文件的代码

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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