how to implement double click in android

前端 未结 9 811
深忆病人
深忆病人 2020-12-05 20:36

I am doing a project in which i want to display a particular message on single touch and another message on double touch using android.How can i implement it.

My sa

相关标签:
9条回答
  • 2020-12-05 21:26

    you can use on long click instead of using double click by override this method

     abstract boolean onLongClick(View v)
    

    Called when a view has been clicked and held.

    0 讨论(0)
  • 2020-12-05 21:27

    I did a simple solution like that -

    buttonTab.setOnClickListener(new View.OnClickListener() {
                int count = 0;
                Handler handler = new Handler();
                Runnable runnable = () -> count = 0;
    
                @Override
                public void onClick(View v) {
                    if (!handler.hasCallbacks(runnable))
                        handler.postDelayed(runnable, 500);
                    if(count==2){
                        /*View is double clicked.Now code here.*/
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-05 21:29

    Try to use GestureDetector.

    public class MyView extends View {
    
    GestureDetector gestureDetector;
    
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // creating new gesture detector
        gestureDetector = new GestureDetector(context, new GestureListener());
    }
    
    // skipping measure calculation and drawing
    
    // delegate the event to the gesture detector
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        //Single Tap
        return gestureDetector.onTouchEvent(e);
    }
    
    
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
    
            Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
    
            return true;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题