Android通过onTouch事件实现单击,双击,长按

一般采用延时做法。


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);

    isDoubleClickfalse;
                    mClickcount0;//移动了
                }
                if(absMx >=5&& absMy >=5) {
                    //处理移动

                    isDoubleClickfalse;
                    mClickcount0;//移动了
                }
                
                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;
    }
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LakeSideHu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值