我在PreferenceActivity中使用SwitchPreference。当我使用SwitchCompat更改开关样式以匹配Lollipop样式时,触摸窗口小部件(显示动画)不会更改SwitchPreference值,而触摸文本列表(无动画)会更改值。
我在Android冰淇淋设备中使用Android支持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);
}
}
}
有什么问题?
答案 0 :(得分:1)
我和你有同样的问题。现在我修好了。只需禁用点击它即可。
@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;
}