Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 棒棒糖的行为变化_Android_Android 5.0 Lollipop_Preferences_Android 5.1.1 Lollipop - Fatal编程技术网

Android 棒棒糖的行为变化

Android 棒棒糖的行为变化,android,android-5.0-lollipop,preferences,android-5.1.1-lollipop,Android,Android 5.0 Lollipop,Preferences,Android 5.1.1 Lollipop,在我们的项目中,我们使用类似于wifi设置的SwitchPreference。用户可以通过单击切换按钮来切换值,用户可以通过单击标题查看更多选项 但这在棒棒糖中不起作用。我可以看到棒棒糖的一些行为改变 在基特卡特: 当用户点击切换按钮时,onPreferenceChanged回调被调用,当用户点击title时,onPreferenceClicked被调用 棒棒糖: 点击切换按钮或titleonPreferenceClicked始终被调用,然后调用onPreferenceChanged。 我怎样才

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

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

在基特卡特:

当用户点击切换按钮时,onPreferenceChanged回调被调用,当用户点击title时,onPreferenceClicked被调用

棒棒糖: 点击切换按钮或titleonPreferenceClicked始终被调用,然后调用onPreferenceChanged
我怎样才能在棒棒糖中得到同样的行为?这种行为破坏了我们的功能。

我遇到了同样的问题,我发现:

经过一番尝试,我发现了如何在我的案例中实施这种变通方法:

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);
      }
  }

}
public类MySwitchPreference扩展了SwitchPreference{
/**
*使用给定的样式选项构造新的SwitchPreference。
*
*@param context将设置此首选项样式的上下文
*@param attrs样式属性与默认值不同
*@param defStyle主题属性定义默认样式选项
*/
公共MySwitchPreference(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
/**
*使用给定的样式选项构造新的SwitchPreference。
*
*@param context将设置此首选项样式的上下文
*@param attrs样式属性与默认值不同
*/
公共MySwitchPreference(上下文、属性集属性){
超级(上下文,attrs);
}
/**
*使用默认样式选项构造新的SwitchPreference。
*
*@param context将设置此首选项样式的上下文
*/
公共MySwitchPreference(上下文){
super(上下文,null);
}
@凌驾
受保护的void onBindView(视图){
ViewGroup ViewGroup=(ViewGroup)视图;
设置可点击开关(视图组);
super.onBindView(视图);
}
私有无效设置开关可点击(视图组视图组){
如果(null==viewGroup){
返回;
}
int count=viewGroup.getChildCount();
对于(int n=0;n

然后,您只需将自己的“MySwitchPreference”直接用于SwitchPreference。

作为一种解决方法,我创建了自定义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);
                }
            }
        });
}

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

不知道为什么,但setClickable对我无效。它对你有效吗?我添加了我使用的解决方法。显然是的,我放在那里的代码为我解决了问题。