Skip to content

Commit ae56f44

Browse files
author
Kaler
committed
Fix bug
1 parent 3af7fb7 commit ae56f44

File tree

1 file changed

+24
-3
lines changed
  • MaterialDesign/src/com/gc/materialdesign/views

1 file changed

+24
-3
lines changed

MaterialDesign/src/com/gc/materialdesign/views/Switch.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ protected void onInitDefaultValues() {
4545
@Override
4646
protected void setAttributes(AttributeSet attrs) {
4747
super.setAttributes(attrs);
48-
getBackground().setAlpha(0);
48+
if (!isInEditMode()) {
49+
getBackground().setAlpha(0);
50+
}
4951
iSchecked = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "checked", false);
5052
eventCheck = iSchecked;
53+
//添加监听器,如果点击了这个控件(不包括ball的区域),这个控件就开始判断是否是开启状态。
5154
setOnClickListener(new OnClickListener() {
5255
@Override
5356
public void onClick(View arg0) {
@@ -63,6 +66,15 @@ public void onClick(View arg0) {
6366
ball = new Ball(getContext());
6467
setThumbParams(size);
6568
addView(ball);
69+
// 给圆球添加监听器,点击圆球后就开始判断是否进入开启状态
70+
ball.setOnClickListener(new OnClickListener() {
71+
72+
@Override
73+
public void onClick(View v) {
74+
// TODO 自动生成的方法存根
75+
setChecked(iSchecked ? false : true);
76+
}
77+
});
6678
}
6779

6880
private void setThumbParams(float size) {
@@ -79,6 +91,7 @@ public boolean onTouchEvent(MotionEvent event) {
7991
if (event.getAction() == MotionEvent.ACTION_DOWN) {
8092
press = true;
8193
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
94+
requestDisallowInterceptTouchEvent(true);
8295
float x = event.getX();
8396
x = (x < ball.xIni) ? ball.xIni : x;
8497
x = (x > ball.xFin) ? ball.xFin : x;
@@ -93,7 +106,9 @@ public boolean onTouchEvent(MotionEvent event) {
93106
isLastTouch = false;
94107
press = false;
95108
}
96-
} else if (event.getAction() == MotionEvent.ACTION_UP) {
109+
} else if (event.getAction() == MotionEvent.ACTION_UP
110+
|| event.getAction() == MotionEvent.ACTION_CANCEL) {
111+
requestDisallowInterceptTouchEvent(false);
97112
press = false;
98113
isLastTouch = false;
99114
if (eventCheck != iSchecked) {
@@ -108,7 +123,13 @@ public boolean onTouchEvent(MotionEvent event) {
108123
}
109124
return true;
110125
}
111-
126+
127+
/* @Override
128+
public boolean onInterceptTouchEvent(MotionEvent ev) {
129+
//return super.onInterceptTouchEvent(ev);
130+
return false;
131+
}
132+
*/
112133
@Override
113134
protected void onDraw(Canvas canvas) {
114135
super.onDraw(canvas);

0 commit comments

Comments
 (0)