[英]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.