Skip to content

Commit 8409302

Browse files
author
ivan.navas
committed
Set OncheckListener to CheckBox and Switch views
1 parent 33dcffb commit 8409302

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
public class CheckBox extends CustomView{
2121

22-
final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";
2322

2423
int backgroundColor = Color.parseColor("#4CAF50");
2524

2625
Check checkView;
2726

2827
boolean press = false;
2928
boolean check = false;
29+
30+
OnCheckListener onCheckListener;
3031

3132

3233
public CheckBox(Context context, AttributeSet attrs) {
@@ -89,6 +90,8 @@ public boolean onTouchEvent(MotionEvent event) {
8990
(event.getY()<= getHeight() && event.getY() >= 0)){
9091
isLastTouch = false;
9192
check = !check;
93+
if(onCheckListener != null)
94+
onCheckListener.onCheck(check);
9295
if(check){
9396
step = 0;
9497
}
@@ -198,5 +201,14 @@ protected void onDraw(Canvas canvas) {
198201

199202
}
200203

204+
public void setOncheckListener(OnCheckListener onCheckListener){
205+
this.onCheckListener = onCheckListener;
206+
}
207+
208+
209+
public interface OnCheckListener{
210+
public void onCheck(boolean check);
211+
}
212+
201213

202214
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.gc.materialdesign.R;
44
import com.gc.materialdesign.utils.Utils;
5+
import com.gc.materialdesign.views.CheckBox.OnCheckListener;
56
import com.nineoldandroids.animation.ObjectAnimator;
67
import com.nineoldandroids.view.ViewHelper;
78

@@ -21,14 +22,16 @@
2122

2223
public class Switch extends CustomView {
2324

24-
final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";
2525

2626
int backgroundColor = Color.parseColor("#4CAF50");
2727

2828
Ball ball;
2929

3030
boolean check = false;
31+
boolean eventCheck = false;
3132
boolean press = false;
33+
34+
OnCheckListener onCheckListener;
3235

3336
public Switch(Context context, AttributeSet attrs) {
3437
super(context, attrs);
@@ -67,7 +70,7 @@ protected void setAttributes(AttributeSet attrs){
6770
}
6871

6972
check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,"check", false);
70-
73+
eventCheck = check;
7174
ball = new Ball(getContext());
7275
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20, getResources()),Utils.dpToPx(20, getResources()));
7376
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
@@ -87,7 +90,6 @@ public boolean onTouchEvent(MotionEvent event) {
8790
x = (x > ball.xFin)? ball.xFin : x;
8891
if(x > ball.xCen){
8992
check = true;
90-
9193
}else{
9294
check = false;
9395
}
@@ -102,10 +104,13 @@ public boolean onTouchEvent(MotionEvent event) {
102104
else if (event.getAction() == MotionEvent.ACTION_UP) {
103105
press = false;
104106
isLastTouch = false;
107+
if(eventCheck != check){
108+
eventCheck = check;
109+
if(onCheckListener != null)
110+
onCheckListener.onCheck(check);
111+
}
105112
if((event.getX()<= getWidth() && event.getX() >= 0) &&
106113
(event.getY()<= getHeight() && event.getY() >= 0)){
107-
// check = !check;
108-
// ball.changeBackground();
109114
ball.animateCheck();
110115
}
111116
}
@@ -218,5 +223,13 @@ public void animateCheck(){
218223

219224

220225
}
226+
227+
public void setOncheckListener(OnCheckListener onCheckListener){
228+
this.onCheckListener = onCheckListener;
229+
}
230+
231+
public interface OnCheckListener{
232+
public void onCheck(boolean check);
233+
}
221234

222235
}

0 commit comments

Comments
 (0)