2

在此处输入图像描述

在我们的项目中,我们使用类似于 wifi 设置的 SwitchPreference。用户可以通过单击切换按钮来切换值,用户可以通过单击标题来查看更多选项。

但这在棒棒糖中不起作用。我可以看到棒棒糖的一些行为变化。

在奇巧:

当用户点击切换按钮时,调用 onPreferenceChanged回调,当用户点击标题时,调用 onPreferenceClicked

在棒棒糖中:您点击切换按钮或标题onPreferenceClicked总是被调用,然后onPreferenceChanged被调用。如何在棒棒糖中获得相同的行为?这种行为正在破坏我们的功能。

4

2 回答 2

1

我遇到了同样的问题,我发现了这个:https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort =&id=172425

经过一番尝试,我找到了如何实施适用于我的案例的解决方法:

public class MySwitchPreference extends SwitchPreference {

/**
 * Construct a new SwitchPreference with the given style options.
 *
 * @param context The Context that will style this preference
 * @param attrs Style attributes that differ from the default
 * @param defStyle Theme attribute defining the default style options
 */
public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/**
 * Construct a new SwitchPreference with the given style options.
 *
 * @param context The Context that will style this preference
 * @param attrs Style attributes that differ from the default
 */
public MySwitchPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * Construct a new SwitchPreference with default style options.
 *
 * @param context The Context that will style this preference
 */
public MySwitchPreference(Context context) {
    super(context, null);
}

@Override
protected void onBindView(View view) {
    ViewGroup viewGroup= (ViewGroup)view;
    setSwitchClickable(viewGroup);
    super.onBindView(view);
}

private void setSwitchClickable(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.setClickable(true);
          return;
      } else if (childView instanceof ViewGroup){
        ViewGroup childGroup = (ViewGroup)childView;
        setSwitchClickable(childGroup);
      }
  }

}

然后你只需要直接在 SwitchPreference 中使用你自己的“MySwitchPreference”。

于 2015-06-22T14:02:15.690 回答
0

作为一种解决方法,我创建了自定义 SwitchPreference 类

public class CustomSwitchPreference extends SwitchPreference {
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomSwitchPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomSwitchPreference(Context context) {
    super(context);
}
 @Override
    protected View onCreateView(ViewGroup parent) {
        View view =  super.onCreateView(parent);
 LinearLayout switchView =  (LinearLayout) ((LinearLayout) view).getChildAt(2);
switchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final boolean newValue = !isChecked();
                if (callChangeListener(newValue)) {
                    setChecked(newValue);
                }
            }
        });
}

它看起来很脏,但对我有用。

于 2015-06-22T14:24:57.693 回答