首页 > 解决方案 > SwitchPreference 点击开关不会触发监听器

问题描述

我正在使用 SwitchPreference 但似乎只有单击首选项视图(行)才会触发首选项单击侦听器或首选项更改侦听器。单击开关本身后,即使开关状态发生变化,也不会调用侦听器。

对此有什么可做的吗?

编辑:我想通了。这是我的听众:

    switchPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            //not called when clicking preference itself
            return true;
        }
    });

这是我的 SwitchPreference:

<SwitchPreference
        android:key="@string/key_email_messages"
        android:layout="@layout/prefs_switch"
        android:title="@string/title_email_messages" />

我正在使用自定义布局来更改首选项的字体大小。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/prefs_row_padding">
<TextView
    android:id="@android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/pref_text_size" />
<Switch
    android:id="@+id/switch_widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

我猜 SwitchPreference 找不到具有该 ID 的开关。我应该使用什么ID?

标签: androidandroid-preferences

解决方案


推荐阅读