SwitchPreference Listener with Custom Layout

后端 未结 3 605
Happy的楠姐
Happy的楠姐 2021-01-14 20:35

I\'m trying to use a SwitchPreference in my Settings page, but i can\'t get the listener to work. I am using a custom widget layout in order to style the switch, as I cannot

3条回答
  •  Happy的楠姐
    2021-01-14 21:30

    The id of SwitchView should be @android:id/switch_widget instead of @+id/custom_switch_item or any other custom id.

    In the source of SwitchPreference class, we can find the code:

    View switchView = view.findViewById(AndroidResources.ANDROID_R_SWITCH_WIDGET);
    

    and in AndroidResources:

    static final int ANDROID_R_SWITCH_WIDGET = android.R.id.switch_widget;
    

    so only id = @android:id/switch_widget can be found correctly.

    Here's a demo about SwitchPreference's widgetLayout:

    
    

提交回复
热议问题