android--双击事件

    private int getCount=0;
    private long firstClick;
    private long secondClick;

    /**
     * 调用写在onToucheEvent中
     * @param event
     */
    private void doubleClick(MotionEvent event){
        if(MotionEvent.ACTION_DOWN == event.getAction()){
            count++;
            if(count == 1){
               firstClick = System.currentTimeMillis();
            } else if (count == 2){
                count = 0;
                secondClick = System.currentTimeMillis();
                if(secondClick - firstClick < 1000){
                    //双击事件
                    Log.i(TAG, "doubleClick: ");
                }else {
                    firstClick = System.currentTimeMillis();
                    count = 1;
                }
            }
        }
    }

看了看参考文章的还有更简洁的:

    private long preClick;
    private long curClick;
    @Override
    public void onDoubleClick(MotionEvent event) {

        if(MotionEvent.ACTION_DOWN == event.getAction()){
            curClick = System.currentTimeMillis();
            if(curClick-preClick<1000){
                //双击事件
                Log.i(TAG, "onDoubleClick: ");
                preClick = 0;
            }else {
                preClick = curClick;
            }
        }
    }

用个GestureDetector类遇到一点麻烦,双击不会调用,而且实现起来写太多东西了,好笨重的感觉

参考博客:http://jasonshieh.iteye.com/blog/751068

你可能感兴趣的:(android)