簡體   English   中英

觸摸SwitchCompat小部件不會更改SwitchPreference值

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

我在PreferenceActivity中使用SwitchPreference。 當我使用SwitchCompat更改開關樣式以匹配Lollipop樣式時,觸摸小部件(顯示動畫)不會更改SwitchPreference的值,而觸摸文本列表(無動畫)會更改該值。

我在我的Android Ice Cream設備中使用Android Support AppCompat-v7:23.1.1,它在Lollipop設備中運行。

這是我的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" />

這是我在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);
        }
    }
}

問題是什么?

我也有同樣的問題 現在,我修復了它。 只需禁用它即可。

@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