通常在android中我们做的是获取对视图的引用,然后在click侦听器上设置然后执行一个函数。 我想做的是,有很多手势(触摸,长时间触摸等),作为参数传递给方法,并根据传递的手势触发一个功能。
这样的事情:
public void doSomething(Event event, View view){
...do something
//if its onClick event that passed do something will trigger once user clicks it
//if its onLongClick event, the method will trigger for the onLongTap event
}
我不知道这样做。任何帮助,将不胜感激。 :)
答案 0 :(得分:0)
您可以实施onTouch
您将拥有以下内容:
public class YourView extends View implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Here you can check for which movement is being made on a switch pattern.
//You can also call any gesture detector you might have
mScaleDetector.onTouchEvent(event);
mRotationDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
final int action = MotionEventCompat.getActionMasked(ev);
switch (action) {
case MotionEvent.ACTION_DOWN:
//Do stuff;
break;
case MotionEvent.ACTION_MOVE:
//Do stuff;
break;
case MotionEvent.ACTION_UP:
//Do stuff;
break;
return false;
}
}
我建议您阅读有关Gesture Detection的文档,它们会为您提供有关如何处理此问题的示例。