2011-07-29 63 views
1

有人可以通过最佳方式来完成此操作吗?我想制作一个扩展为OnTouchListener的界面。而不是一个叫做onTouch(View view, MotionEvent e)的meathod,我想要3个meathods; onPress()onMove()onRelease()。当您在屏幕上按下时onPress meathod被调用,当您在屏幕上移动手指时onMove被调用。当你松开你的手指时,onRelease被调用。欢迎所有相关答案。在Android中创建界面

+0

也许所有你需要的是http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html –

回答

3

你会使用由duffymo提供的YourTouchListener扩展您的View类和添加setYourTouchListener(YourTouchListener listener)方法到这个类。

然后,您会覆盖onTouchEvent(MotionEvent event)并调用您的侦听器的相关方法。像这样:

public boolean onTouchEvent(MotionEvent event) { 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      listener.onPress(this, event); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      listener.onMove(this, event); 
      break; 
     case MotionEvent.ACTION_UP: 
      listener.onRelease(this, event); 
      break; 
    } 
    //this means that you have "used" this event. The ViewGroup will direct all further associated events straight here. 
    return true; 
} 
+0

@parkovski有一个好点 - 而不是扩展OnTouchListener它可能更好只是创建自己的接口用你想要的方法。 –

1

您可以使用GestureListener和使用onFling(...)方法,它为您提供了可能性(MotionEvent e1MotionEvent e2)来衡量初始触摸(在印刷机上)和最后的接触(释放),并在此基础上,你做你的工作。还提供velocityMotionEvent沿xy轴,测量施加在屏幕上的压力等。这将减少您在编写您将要做的整个界面的时间。

1

我可能会做基本上是什么CaspNZ发布,唯一的区别是,你不应该在这种情况下延伸OnTouchListener。实现一个接口意味着你必须给它的方法实现全部,所以在这种情况下,除了你正在创建的三个,这是多余的onTouch。当然,如果您仍然需要onTouch来完成某项任务,除了YourTouchListener之外,您始终可以执行OnTouchListener