具有自定义布局的SwitchPreference侦听器 [英] SwitchPreference Listener with Custom Layout
问题描述
我正在尝试在设置页面中使用SwitchPreference,但无法使监听器正常工作。我正在使用自定义窗口小部件布局来设置开关的样式,因为我找不到如何使用主题来实现此目的。在我的settings.xml中,我具有以下内容:
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>
,我的SettingsFragment如下:
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?
推荐答案
我遇到了同样的问题,我的解决方案类似于以下内容:
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
android:clickable="false" android:focusable="false"
扩展 SwitchPreference,并在 onBindView类中,从SharedPreferences获取状态,并在那里处理开关状态更改。
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.
这篇关于具有自定义布局的SwitchPreference侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!