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