简体   繁体   中英

catch double click on textview android

i want to catch double click on textview for that i have used below code

but it still not working :(

TextView txtOne;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtOne = (TextView) findViewById(R.id.txtOne);
    txtOne.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("DRAG");
            return gestureDetector.onTouchEvent(event);

        }
    });
}

final GestureDetector gestureDetector = new GestureDetector(
        new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                System.out.println("Double Tap");
                return super.onDoubleTap(e);
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                System.out.println("One Click");
                return super.onSingleTapUp(e);
            }


        });

only drag is calling but not "Double Tap" and "One Click" never called

Try following steps.

Step 1

Write following code in your activity.

// initialize the Gesture Detector
gd = new GestureDetector(this,new OnGestureListener() {
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
});

// set the on Double tap listener
gd.setOnDoubleTapListener(new OnDoubleTapListener() {
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Toast.makeText(SplashActivity.this,"Double Tap",Toast.LENGTH_LONG).show();
    return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // if the second tap hadn't been released and it's being moved

        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

});

Step 2

Write following code for activity. Here gd will be GestureDetector object.

txt.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            gd.onTouchEvent(event);
            return false;
        }
    });

For SimpleGestureDetector , you need to override onDown() and return true to trigger double tap detector.

Whether or not you use GestureDetector.OnGestureListener , it's best practice to implement an onDown() method that returns true . This is because all gestures begin with an onDown() message. If you return false from onDown() , as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.

Source: http://developer.android.com/training/gestures/detector.html

这是一个很好的网站,可以进行双击...我用它并且工作了doubleCLICK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM