Android 如何在 PreferenceScreen 中使用自定义复选框

How to use custom CheckBox in PreferenceScreen on Android

我想在我的应用程序中使用 PreferenceScreen 来设置一些设置。我已经定制了这个页面,并为任何偏好设置了布局。所有的偏好都可以,但是CheckBoxPreferences,当我点击它时没有任何反应,每次都是这样。我可以设置是假的!!!我已经为自定义 CheckBox 设置了 android:id="@+android:id/checkbox"
复选框自定义 xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"
        android:layout_marginTop="5dp"
        android:gravity="right"
        android:textColor="#000"
        android:textSize="18sp" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+android:id/title"
        android:layout_marginRight="16dp"
        android:layout_marginTop="5dp"
        android:gravity="right"
        android:textColor="#c0c0c0"
        android:textSize="13sp" />

    <CheckBox
        android:id="@+android:id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="16dp"/>

</RelativeLayout>

首选项屏幕 xml :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:example="http://schemas.android.com/apk/res-auto"
    xmlns:sample="http://schemas.android.com/apk/res-auto">

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="setting_main_subtitle_show"
            android:layout="@layout/pref_checkbox_layout"
            android:summary="Summary Text"
            android:title="Title" />

</PreferenceScreen>

AppPreference java 代码:

public class AppPreference {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Context context;

    private static final String KeyShowSubTitle = "setting_main_subtitle_show";

    public AppPreference(Context context){
        this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        this.editor = sharedPreferences.edit();
        this.context = context;
    }

    public Boolean getShowSubTitle(){
        return sharedPreferences.getBoolean(KeyShowSubTitle, true);
    }
}

主页java代码:

Boolean show_subTitle = appPreference.getShowSubTitle();
if (show_subTitle == true){
    /// SubTitle Text
    toolbar.setSubtitle(appPreference.getSubTitleText());
} else {
    toolbar.setSubtitle("");
}

如何解决此问题并在 PreferenceScreen 中使用自定义 CheckBox

您可以使用setTag()getTag()方法来解决这个问题。 从您的 xml 文件中,将 android:tag="integer" 添加到 CheckBoxPreference 并使用 getMethod 在您的 java 文件中获取该整数并执行您的操作并使用 setMethod 然后将其保存在共享首选项中。

试试这个

custom_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="true" 
android:button="@drawable/android_button"/>

default.xml

<CheckBoxPreference 
 android:key="@string/Drop_Option"
 android:title="Close after call drop" 
 android:defaultValue="true"
 android:widgetLayout="@layout/custom_layout"/>