gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:31:57 25 4
gpt4 key购买 nike

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

关于android - 触摸 SwitchCompat 小部件不会更改 SwitchPreference 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com