Có một workaround mà tôi không chắc chắn cách đầy đủ nó là, bằng cách gói mà xem để sử dụng trong các CheckBoxPreference (có thể bỏ lỡ một số chức năng, nhưng trong sử dụng chung, nó hoạt động).
Giải pháp thay thế sẽ sử dụng CheckBoxPreference cho pre-API-14 và SwitchPreference cho API 14 trở lên.
Dưới đây là các mã:
public class SwitchPreference extends CheckBoxPreference
{
android.preference.SwitchPreference _switchPreference =null;
public SwitchPreference(final Context context)
{
super(context);
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
_switchPreference=new android.preference.SwitchPreference(context);
}
public SwitchPreference(final Context context,final AttributeSet attrs)
{
super(context,attrs);
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
_switchPreference=new android.preference.SwitchPreference(context,attrs);
}
public SwitchPreference(final Context context,final AttributeSet attrs,final int defStyle)
{
super(context,attrs,defStyle);
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
_switchPreference=new android.preference.SwitchPreference(context,attrs,defStyle);
}
@Override
protected View onCreateView(final ViewGroup parent)
{
final View view;
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
{
view=_switchPreference.getView(null,parent);
// set as checked the view and the view's children, each in case it extend from Checkable
ViewUtil.setChecked(view,isChecked());
// set as non-clickable the view and the view's children
ViewUtil.setClickable(view,false);
}
else view=super.onCreateView(parent);
return view;
}
Nguồn
2013-04-06 19:35:37
Có tương đương với thẻ "bao gồm" của các tệp công khai chưa, đối với tệp tùy chọn? Bằng cách này, chỉ có văn bản tối thiểu sẽ là cần thiết vì đồng bằng sẽ được đặt trong thư mục v14, phải không? –
@androiddeveloper: Xin lỗi, không phải là tôi biết. – CommonsWare
Tôi đã thực hiện một giải pháp tốt cho vấn đề này, kết thúc tốt đẹp switchPreference khi có thể và sử dụng hộp kiểm nếu không thể. Bạn có thể kiểm tra xem nó có ổn không? theo thử nghiệm của tôi, nó hoạt động tốt, nhưng có lẽ tôi đã bỏ lỡ một số chức năng. –