【问题标题】:Android 5.x SwitchPreference is not behaving the same as in Android 4.xAndroid 5.x SwitchPreference 的行为与 Android 4.x 不同
【发布时间】:2015-06-22 13:09:35
【问题描述】:

我有一个使用 SwitchPreference 的代码,该代码用于 Android 4.x,但是自从我将设备更新到 Android 5.0.1 后它不再有效。

我有一个简单的SwitchPreference,它在左侧显示一个标题,在右侧显示一个 ON/OFF 开关。

    <SwitchPreference
        android:key="myPref"            
        android:selectable="true"
        android:title="Title" 
        android:fragment="com.myApp.DeviceMonitorPrefsActivity"            
        android:switchTextOn="ON"
        android:switchTextOff="OFF"/>

在 PreferenceActivity 上,当我单击此 SwitchPreference 控件的标题时,我覆盖了 onPreferenceTreeClick() 以执行操作(在我的情况下启动设置活动)。

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) 
    {
        if(preference instanceof SwitchPreference){
            // My Action
        }
    }

在 Android 4.4.4 中,该 Action 仅在我按下此控件(标题)的左侧时才会执行,而不是在我更改开关状态时执行。

现在在 Android 5.0.1 中,即使我更改了开关状态,也会调用 onPreferenceTreeClick(),但我没有找到区分这两种情况的方法。

这是 Android 5.0.1 中的一个错误,还是有办法让这项工作干净利落地?

【问题讨论】:

标签: android-5.0-lollipop switchpreference


【解决方案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);
      }
  }

}

那么你只需要直接使用你自己的“MySwitchPreference”进入SwitchPreference。

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多