简体   繁体   中英

Touch on SwitchCompat widget doesn't change SwitchPreference value

I use SwitchPreference in my PreferenceActivity. 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.

I use Android Support AppCompat-v7:23.1.1 in my Android Ice Cream device, it works in Lollipop device.

Here my 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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