如何在onTouch()中获得单击已确认事件?

时间:2018-04-12 06:04:50

标签: android ontouchlistener

其实我想在onTouch方法中获取singleTapConfirmed()事件。众所周知,我们可以为此目的使用gestureDetector类,但我想知道如何使用onTouch

我不想打电话GestureDetector#onTouchEvent我想手动检测。

下面的代码是this answer的双击,但如何在此代码中获取singleTapConfirmed

DoubleTap代码:

imageView.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View view, MotionEvent event) {
               switch(event.getAction() & MotionEvent.ACTION_MASK)
               {
                   case MotionEvent.ACTION_DOWN:
                       startTime = System.currentTimeMillis();
                       clickCount++;
                       break;
                   case MotionEvent.ACTION_UP:
                       long time = System.currentTimeMillis() - startTime;
                       duration=  duration + time;
                       if(clickCount == 2)
                       {
                           if(duration<= MAX_DURATION)
                           {
                               Toast.makeText(imageView.getContext(), "double tap",Toast.LENGTH_LONG).show();
                           }
                           clickCount = 0;
                           duration = 0;
                           break;
                       }
               }
               return true;
           }
       });

2 个答案:

答案 0 :(得分:0)

试试这个

 imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           startTime = System.currentTimeMillis();
                   clickCount++;

        }
    });

OnTouchListner返回falsh所以删除触摸事件

imageView.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View view, MotionEvent event) {
           switch(event.getAction() & MotionEvent.ACTION_MASK)
           {

               case MotionEvent.ACTION_UP:
                   long time = System.currentTimeMillis() - startTime;
                   duration=  duration + time;
                   if(clickCount == 2)
                   {
                       if(duration<= MAX_DURATION)
                       {
                           Toast.makeText(imageView.getContext(), "double 
                           tap",Toast.LENGTH_LONG).show();
                       }
                       clickCount = 0;
                       duration = 0;
                       break;
                   }
           }
           return false;
       }
   });

答案 1 :(得分:0)

使用onTouch监听器进行手势检测会很好,如下所示

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
     //do something
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        super.onLongPress(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return super.onDoubleTap(e);
    }
});

并使用gestureDetector作为

 viewToTouch.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return gestureDetector.onTouchEvent(event);
    }
});