1、定义Switch类,继承CompoundButton
public class BlueSwitch extends CompoundButton {
private Drawable switchOn;
private Drawable switchOff;
private boolean mChecked;
private OnCheckedChangeListener mOnCheckedChangeListener;
public interface OnCheckedChangeListener {
public void onCheckedChanged(BlueSwitch switchView, boolean isChecked);
}
public BlueSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BlueSwitch, 0, 0);
try {
switchOn = a.getDrawable(R.styleable.BlueSwitch_switchOn);
switchOff = a.getDrawable(R.styleable.BlueSwitch_switchOff);
}catch (Exception e) {
e.printStackTrace();
}
a.recycle();
}
public BlueSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs