[英]How to use OnTouchListener to keep track of ACTION_DOWN and ACTION_UP (x,y) coordinates?
I'm new to android studio. 我是android studio的新手。 In my app I have a button where I want to get the x-coord and y-coord of both where the button is clicked and released.
在我的应用程序中,我有一个按钮,我想获得单击和释放按钮的x坐标和y坐标。 I put the following code in the onCreate() method and I'm not sure where to go from here.
我将以下代码放入onCreate()方法中,但不确定从何处去。
btn1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x1,x2,y1,y2;
switch(event.getAction()){
case(MotionEvent.ACTION_DOWN):{
x1 = event.getX();
y1 = event.getY();
Log.d("Pressed 1", "x1=" + x1 + "y1=" + y1);
break;
}
case(MotionEvent.ACTION_UP):
y2=event.getY();
Log.d("Released 1", "x2=" + x2 + "y2=" + y2);
}
return false;
}
});
You should return true
, or the following MotionEvent will not going to this onTouch
callback. 您应该
return true
,否则以下MotionEvent将不会进入此onTouch
回调。
If you need the moving event, case MotionEvent.ACTION_MOVE
also. 如果需要移动事件,请同时设置
MotionEvent.ACTION_MOVE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.