package com.example.longpress; /** * @author 作者 E-mail: chenshaohua2012@126.com * @version 创建时间:2013-1-15 下午12:07:32 * 类说明 */ import android.content.Context; import android.view.MotionEvent; import android.view.View; public class LongPressView1 extends View { private int mLastMotionX, mLastMotionY; // 是否移动了 private boolean isMoved; // 是否释放了 private boolean isReleased; // 计数器,防止多次点击导致最后一次形成longpress的时间变短 private int mCounter; // 长按的runnable private Runnable mLongPressRunnable; // 移动的阈值 private static final int TOUCH_SLOP = 20; public LongPressView1(Context context) { super(context); mLongPressRunnable = new Runnable() { @Override public void run() { System.out.println("thread"); System.out.println("mCounter--->>>"+mCounter); System.out.println("isReleased--->>>"+isReleased); System.out.println("isMoved--->>>"+isMoved); mCounter--; // 计数器大于0,说明当前执行的Runnable不是最后一次down产生的。 if (mCounter > 0 || isReleased || isMoved) return; performLongClick();// 回调长按事件 } }; } public boolean dispatchTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; mCounter++; isReleased = false; isMoved = false; postDelayed(mLongPressRunnable, 3000);// 按下 3秒后调用线程 break; case MotionEvent.ACTION_MOVE: if (isMoved) break; if (Math.abs(mLastMotionX - x) > TOUCH_SLOP || Math.abs(mLastMotionY - y) > TOUCH_SLOP) { // 移动超过阈值,则表示移动了 isMoved = true; } break; case MotionEvent.ACTION_UP: // 释放了 isReleased = true; break; } return true; } }
public class LongMainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout.LayoutParams layout = new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); View v = new LongPressView1(LongMainActivity.this); v.setLayoutParams(layout); setContentView(v); v.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(LongMainActivity.this, "abcdef", Toast.LENGTH_SHORT).show(); return false; } }); } }