Android自定義長按事件


 Android系統自帶了長按事件,setOnLongClickListener即可監聽。但是有時候,你不希望用系統的長按事件,比如當希望長按的時間更長一點的時候。這時候就需要自己來定義這個長按事件了。
    下面是去年我寫代碼的時候,自定義長按事件的方式:

Java代碼  收藏代碼
  1. package chroya.fun;  
  2.   
  3. import android.content.Context;  
  4. import android.view.MotionEvent;  
  5. import android.view.View;  
  6. import android.view.ViewConfiguration;  
  7.   
  8. public class LongPressView1 extends View{  
  9.     private int mLastMotionX, mLastMotionY;  
  10.     //是否移動了  
  11.     private boolean isMoved;  
  12.     //是否釋放了  
  13.     private boolean isReleased;  
  14.     //計數器,防止多次點擊導致最后一次形成longpress的時間變短  
  15.     private int mCounter;  
  16.     //長按的runnable  
  17.     private Runnable mLongPressRunnable;  
  18.     //移動的閾值  
  19.     private static final int TOUCH_SLOP = 20;  
  20.   
  21.     public LongPressView1(Context context) {  
  22.         super(context);  
  23.         mLongPressRunnable = new Runnable() {  
  24.               
  25.             @Override  
  26.             public void run() {  
  27.                 mCounter--;  
  28.                 //計數器大於0,說明當前執行的Runnable不是最后一次down產生的。  
  29.                 if(mCounter>0 || isReleased || isMoved) return;  
  30.                 performLongClick();  
  31.             }  
  32.         };  
  33.     }  
  34.   
  35.     public boolean dispatchTouchEvent(MotionEvent event) {  
  36.         int x = (int) event.getX();  
  37.         int y = (int) event.getY();  
  38.           
  39.         switch(event.getAction()) {  
  40.         case MotionEvent.ACTION_DOWN:  
  41.             mLastMotionX = x;  
  42.             mLastMotionY = y;  
  43.             mCounter++;  
  44.             isReleased = false;  
  45.             isMoved = false;  
  46.             postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());  
  47.             break;  
  48.         case MotionEvent.ACTION_MOVE:  
  49.             if(isMoved) break;  
  50.             if(Math.abs(mLastMotionX-x) > TOUCH_SLOP   
  51.                     || Math.abs(mLastMotionY-y) > TOUCH_SLOP) {  
  52.                 //移動超過閾值,則表示移動了  
  53.                 isMoved = true;  
  54.             }  
  55.             break;  
  56.         case MotionEvent.ACTION_UP:  
  57.             //釋放了  
  58.             isReleased = true;  
  59.             break;  
  60.         }  
  61.         return true;  
  62.     }  
  63. }  

     代碼里注釋的比較清楚。主要思路是在down的時候,讓一個Runnable一段時間后執行,如果時間到了,沒有移動超過定義的閾值,也沒有釋放,則觸發長按事件。在真實環境中,當長按觸發之后,還需要將后來的move和up事件屏蔽掉。此處是示例,就略去了。

      下面講講第二種方式:

Java代碼  收藏代碼
  1. package chroya.fun;  
  2.   
  3. import android.content.Context;  
  4. import android.view.MotionEvent;  
  5. import android.view.View;  
  6. import android.view.ViewConfiguration;  
  7.   
  8. public class LongPressView2 extends View{  
  9.     private int mLastMotionX, mLastMotionY;  
  10.     //是否移動了  
  11.     private boolean isMoved;  
  12.     //長按的runnable  
  13.     private Runnable mLongPressRunnable;  
  14.     //移動的閾值  
  15.     private static final int TOUCH_SLOP = 20;  
  16.   
  17.     public LongPressView2(Context context) {  
  18.         super(context);  
  19.         mLongPressRunnable = new Runnable() {  
  20.               
  21.             @Override  
  22.             public void run() {               
  23.                 performLongClick();  
  24.             }  
  25.         };  
  26.     }  
  27.   
  28.     public boolean dispatchTouchEvent(MotionEvent event) {  
  29.         int x = (int) event.getX();  
  30.         int y = (int) event.getY();  
  31.           
  32.         switch(event.getAction()) {  
  33.         case MotionEvent.ACTION_DOWN:  
  34.             mLastMotionX = x;  
  35.             mLastMotionY = y;  
  36.             isMoved = false;  
  37.             postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());  
  38.             break;  
  39.         case MotionEvent.ACTION_MOVE:  
  40.             if(isMoved) break;  
  41.             if(Math.abs(mLastMotionX-x) > TOUCH_SLOP   
  42.                     || Math.abs(mLastMotionY-y) > TOUCH_SLOP) {  
  43.                 //移動超過閾值,則表示移動了  
  44.                 isMoved = true;  
  45.                 removeCallbacks(mLongPressRunnable);  
  46.             }  
  47.             break;  
  48.         case MotionEvent.ACTION_UP:  
  49.             //釋放了  
  50.             removeCallbacks(mLongPressRunnable);  
  51.             break;  
  52.         }  
  53.         return true;  
  54.     }  
  55. }  

     思路跟第一種差不多,不過,在移動超過閾值和釋放之后,會將Runnable從事件隊列中remove掉,長按事件也就不會再觸發了。源碼中實現長按的原理也基本如此。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM