一般采用延时做法。
private intmClickcount;// 点击次数
private int mDownX;
private int mDownY;
private int mMoveX;
private int mMoveY;
private int mUpX;
private int mUpY;
private long mLastDownTime;
private long mLastUpTime;
private long mFirstClick;
private long mSecondClick;
private boolean isDoubleClick=false;
private int MAX_LONG_PRESS_TIME=350;// 长按/双击最长等待时间
private int MAX_SINGLE_CLICK_TIME=50;// 单击最长等待时间
private int MAX_MOVE_FOR_CLICK=50;// 最长改变距离,超过则算移动
private HandlermBaseHandler=newHandler();
private RunnablemLongPressTask=newRunnable() {
@Override
public voidrun() {
//处理长按
mClickcount=0;
}
};
private RunnablemSingleClickTask=newRunnable() {
@Override
public voidrun() {
// 处理单击
mClickcount=0;
}
};
myView.setOnTouchListener(newView.OnTouchListener() {
@Override
public booleanonTouch(View v,MotionEvent event) {
switch(event.getAction()) {
caseMotionEvent.ACTION_DOWN:
mLastDownTime= System.currentTimeMillis();
mDownX= (int) event.getX();
mDownY= (int) event.getY();
mClickcount++;
Log.e("mouse","DOWN-->mClickcount="+mClickcount+"; isDoubleClick="+isDoubleClick);
if (mSingleClickTask!=null) {
mBaseHandler.removeCallbacks(mSingleClickTask);
}
if(!isDoubleClick)mBaseHandler.postDelayed(mLongPressTask,MAX_LONG_PRESS_TIME);
if (1==mClickcount) {
mFirstClick= System.currentTimeMillis();
}else if(mClickcount==2) {// 双击
mSecondClick= System.currentTimeMillis();
if (mSecondClick-mFirstClick<=MAX_LONG_PRESS_TIME) {
//处理双击
isDoubleClick=true;
mClickcount=0;
mFirstClick=0;
mSecondClick=0;
mBaseHandler.removeCallbacks(mSingleClickTask);
mBaseHandler.removeCallbacks(mLongPressTask);
Log.e("mouse","double double double....");
}
}
break;
case MotionEvent.ACTION_MOVE:
mMoveX= (int) event.getX();
mMoveY= (int) event.getY();
int absMx = Math.abs(mMoveX-mDownX);
int absMy = Math.abs(mMoveY-mDownY);
Log.e("mouse","MOVE-->absMx="+ absMx +"; absMy="+ absMy);
if (absMx >MAX_MOVE_FOR_CLICK&& absMy >MAX_MOVE_FOR_CLICK) {
mBaseHandler.removeCallbacks(mLongPressTask);
mBaseHandler.removeCallbacks(mSingleClickTask);
isDoubleClick= false;
mClickcount= 0;//移动了
}
if(absMx >=5&& absMy >=5) {
//处理移动
isDoubleClick= false;
mClickcount= 0;//移动了
}
break;
case MotionEvent.ACTION_UP:
mLastUpTime= System.currentTimeMillis();
mUpX= (int) event.getX();
mUpY= (int) event.getY();
int mx = Math.abs(mUpX-mDownX);
int my = Math.abs(mUpY-mDownY);
Log.e("mouse","UP-->mx="+ mx +"; my="+ my);
if (mx <=MAX_MOVE_FOR_CLICK&& my <=MAX_MOVE_FOR_CLICK) {
if((mLastUpTime-mLastDownTime) <=MAX_LONG_PRESS_TIME) {
mBaseHandler.removeCallbacks(mLongPressTask);
if (!isDoubleClick)mBaseHandler.postDelayed(mSingleClickTask,MAX_SINGLE_CLICK_TIME);
}else{
//超出了双击间隔时间
mClickcount=0;
}
} else{
//移动了
mClickcount=0;
}
if(isDoubleClick)isDoubleClick=false;
taiqi();
break;
}
return true;
}
});