Tob*_*sen 24 android ontouchlistener onclicklistener
我需要做一些事情,当用户点击ImageButton我试图创建一个实现静态类都 OnClickListener和OnTouchListener
static class ClickListenerForScrolling implements OnClickListener, OnTouchListener
Run Code Online (Sandbox Code Playgroud)
有以下方法:
@Override
public void onClick(View v)
Run Code Online (Sandbox Code Playgroud)
和
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
Run Code Online (Sandbox Code Playgroud)
这背后的整个想法是更改ImageButton用户触摸它时的图像源,并在用户单击此按钮时执行任务.任何人都可以给我一个如何做到这一点的提示吗?
Sve*_*ner 36
由于这两个动作都包括"将手指放在屏幕上 - 手指从屏幕上抬起"的手势,因此无法确定是触摸动作还是单击动作.因此,如果您在此图像按钮上实现两个侦听器,则触摸/单击将更改图片并按下按钮.不确定这些事件是否有确定的顺序......
但是,如果要分离这些事件,则需要为其中一个操作定义不同的手势(例如擦除以更改图片),或创建处理事件的不同区域,例如图像不适合整个按钮和空闲区域用作按钮点击区域.
HTH
更新:
我想通了,a TouchEvent比一般更通用,ClickEvent因此它首先被称为.
public abstract boolean onTouch (View v, MotionEvent event)
Run Code Online (Sandbox Code Playgroud)
如果侦听器已使用该事件,则返回true,否则返回false.因此,您可以在实现中决定是否也应该由OnClickListener处理事件,然后返回false.
小智 12
的onTouchListener是能够同时处理的动作.
Switch在event.action()值之间获得MotionEvent.
case MotionEvent.ACTION_DOWN:是第一次手指撞击.
case MotionEvent.ACTION_UP:是手指消失的时候.
你必须设置影响点ACTION_DOWN.
TheImpactPoint=event.getX();
然后获得距离 ACTION_UP
float distance =TheImpactPoint-event.getX();
If distance = 0 然后有一个点击,否则根据手势它会多于或少于零.
因此,这是在没有真正点击事件的情况下使用点击事件并仅使用onTouchListener.
希望会有用.
小智 11
使用此代码:
public class GestureHelper implements OnTouchListener {
private final GestureDetector mGestureDetector;
public GestureHelper(Context context) {
mGestureDetector = new GestureDetector(context, new GestureListener(this));
}
public void onSwipeRight() {
};
public void onSwipeLeft() {
};
public void onSwipeTop() {
};
public void onSwipeBottom() {
};
public void onDoubleTap() {
};
public void onClick() {
};
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private static final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
private GestureHelper mHelper;
public GestureListener(GestureHelper helper) {
mHelper = helper;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
mHelper.onClick();
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
mHelper.onDoubleTap();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
mHelper.onSwipeRight();
} else {
mHelper.onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
mHelper.onSwipeBottom();
} else {
mHelper.onSwipeTop();
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
}
Run Code Online (Sandbox Code Playgroud)
扩展这个类并像这样使用...
view.setOnTouchListener(new SomeYourGestureHelper(context, someParameters));
Run Code Online (Sandbox Code Playgroud)