问题
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 find how to use themes to achieve this. In my settings.xml i have the following:
<SwitchPreference
android:key="uitestmode"
android:summary="Enable to display test data in fragments"
android:title="UI Test Mode"
android:widgetLayout="@layout/widget_custom_switch_on_off" >
</SwitchPreference>
and my SettingsFragment looks like:
SwitchPreference uiTestModePref = (SwitchPreference) findPreference("uitestmode");
uiTestModePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(preference.getKey(), newValue.toString());
return false;
}
});
and my custom layout for my switch:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textOn="On"
android:textOff="Off"
android:textStyle="bold"
android:thumb="@drawable/carcast_switch_inner_holo_dark"
android:track="@drawable/carcast_switch_track_holo_dark" />
The problem I have is that when I click the switch, the listener is not called. Does anybody know why and how to fix it?
回答1:
I ran into the same issue, my solution is similar to the followings:
In your custom switch layout, add following code. This "uncovers" the preference, and makes the whole block clickable. That's why the listener is not triggered at all.
android:clickable="false" android:focusable="false"
Extend "SwitchPreference", and in "onBindView" class, get states from SharedPreferences, and handle switch states change there.
public class MySwitchPreference extends SwitchPreference { public MySwitchPreference(Context context) { super(context, null); } public MySwitchPreference(Context context, AttributeSet attrs) { super(context, attrs); } public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } protected void onBindView(View view) { // Clean listener before invoke SwitchPreference.onBindView ViewGroup viewGroup= (ViewGroup)view; clearListenerInViewGroup(viewGroup); super.onBindView(view); final Switch mySwitch = (Switch) view.findViewById(R.id.custom_switch_item); Boolean initVal = this.getPersistedBoolean(false); if (initVal) { mySwitch.setChecked(true); } this.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { //Do your things here, toggling the switch and so on return true; } }); } private void clearListenerInViewGroup(ViewGroup viewGroup) { if (null == viewGroup) { return; } int count = viewGroup.getChildCount(); for(int n = 0; n < count; ++n) { View childView = viewGroup.getChildAt(n); if(childView instanceof Switch) { final Switch switchView = (Switch) childView; switchView.setOnCheckedChangeListener(null); return; } else if (childView instanceof ViewGroup){ ViewGroup childGroup = (ViewGroup)childView; clearListenerInViewGroup(childGroup); } } } }
Hopefully this can help you. I was not able to find any solution for my case until I figured it out this way.
回答2:
Solution – based on @RickCase answer, but works with both Switch and SwitchCompat.
来源:https://stackoverflow.com/questions/24655834/switchpreference-listener-with-custom-layout