简体   繁体   English

触摸SwitchCompat小部件不会更改SwitchPreference值

[英]Touch on SwitchCompat widget doesn't change SwitchPreference value

I use SwitchPreference in my PreferenceActivity. 我在PreferenceActivity中使用SwitchPreference。 When I change switch style to match Lollipop style using SwitchCompat, touch on widget (animation is shown) doesn't change SwitchPreference value while touch on text list (no animation) change the value. 当我使用SwitchCompat更改开关样式以匹配Lollipop样式时,触摸小部件(显示动画)不会更改SwitchPreference的值,而触摸文本列表(无动画)会更改该值。

I use Android Support AppCompat-v7:23.1.1 in my Android Ice Cream device, it works in Lollipop device. 我在我的Android Ice Cream设备中使用Android Support AppCompat-v7:23.1.1,它在Lollipop设备中运行。

Here my preference.xml 这是我的preference.xml

<SwitchPreference
    android:defaultValue="false"
    android:key="enable stock"
    android:summary="POS can display and decrease stock if any transaction occurred"
    android:title="Enable stock" />

Here my onCreateView() method in SettingsActivity.java to use Lollipop style, use SwitchCompat() method 这是我在SettingsActivity.java中的 onCreateView()方法要使用Lollipop样式,请使用SwitchCompat()方法

@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    // Allow super to try and create a view first
    final View result = super.onCreateView(name, context, attrs);
    if (result != null) {
        return result;
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // If we're running pre-L, we need to 'inject' our tint aware Views in place of the
        // standard framework versions
        switch (name) {
            case "EditText":
                return new AppCompatEditText(this, attrs);
            case "Spinner":
                return new AppCompatSpinner(this, attrs);
            case "CheckBox":
                return new AppCompatCheckBox(this, attrs);
            case "RadioButton":
                return new AppCompatRadioButton(this, attrs);
            case "CheckedTextView":
                return new AppCompatCheckedTextView(this, attrs);
            case "Switch":
                return new SwitchCompat(this, attrs);
        }
    }
}

What is the problem? 问题是什么?

I have same problem with you. 我也有同样的问题 And now I fixed it. 现在,我修复了它。 Just disable clicking on it. 只需禁用它即可。

@Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        // Allow super to try and create a view first
        final View result = super.onCreateView(name, context, attrs);
        if (result != null) {
            return result;
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // If we're running pre-L, we need to 'inject' our tint aware Views in place of the
            // standard framework versions
            switch (name) {
                case "EditText":
                    return new AppCompatEditText(this, attrs);
                case "Spinner":
                    return new AppCompatSpinner(this, attrs);
                case "CheckBox":
                    return new AppCompatCheckBox(this, attrs);
                case "RadioButton":
                    return new AppCompatRadioButton(this, attrs);
                case "CheckedTextView":
                    return new AppCompatCheckedTextView(this, attrs);
                case "Switch":
                    SwitchCompat switchCompat = new SwitchCompat(this, attrs);
                    switchCompat.setFocusableInTouchMode(false);
                    switchCompat.setClickable(false);
                    switchCompat.setFocusable(false);
                    return switchCompat;

            }
        }

        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM