2011-07-29 62 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