66

在我的应用程序中,我希望发生两件事。

  1. 当我触摸并拖动 ImageButton 时,它应该与我的手指一起移动。

    我用过OnTouchListener()这个,它工作正常。

  2. 当我单击 ImageButton 时,它应该关闭活动。

    我用过OnClickListener()这个,它也很好用。

所以,这是我的问题。每当我移动ImageButton OnTouchListenertirggered 并且ImageButton移动时,OnClickListener当我从移动中释放按钮时也会在最后触发。

如何在同一个按钮上使用 ontouch 和 onclick 监听器而不互相干扰?

4

12 回答 12

139

Try this, It may help you

No need to set onClick() method onTouch() will handle both the case.

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class MainActivity extends Activity {
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, new SingleTapConfirm());
        ImageButton imageButton = (ImageButton) findViewById(R.id.img);

        imageButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (gestureDetector.onTouchEvent(arg1)) {
                    // single tap
                    return true;
                } else {
                    // your code for move and drag
                }

                return false;
            }
        });

    }

    private class SingleTapConfirm extends SimpleOnGestureListener {

        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            return true;
        }
    }

}
于 2013-10-23T10:30:46.293 回答
35

要在 Single 上拥有Click Listener, DoubleClick Listener, OnLongPress Listener, Swipe Left, Swipe Right, Swipe Up,您需要. IE,Swipe DownViewsetOnTouchListener

view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {

            @Override
            public void onClick() {
                super.onClick();
                // your on click here
            }

            @Override
            public void onDoubleClick() {
                super.onDoubleClick();
                // your on onDoubleClick here
            }

            @Override
            public void onLongClick() {
                super.onLongClick();
                // your on onLongClick here
            }

            @Override
            public void onSwipeUp() {
                super.onSwipeUp();
                // your swipe up here
            }

            @Override
            public void onSwipeDown() {
                super.onSwipeDown();
                // your swipe down here.
            }

            @Override
            public void onSwipeLeft() {
                super.onSwipeLeft();
                // your swipe left here.
            }

            @Override
            public void onSwipeRight() {
                super.onSwipeRight();
                // your swipe right here.
            }
        });

}

为此,您需要OnSwipeTouchListener实现OnTouchListener.

public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

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

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

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

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {

}

public void onDoubleClick() {

}

public void onLongClick() {

}
}
于 2015-04-16T13:45:34.923 回答
9

onClick 和 OnTouch 事件的问题在于,当您单击(意图单击)时,它假定事件为 OnTouch,因此永远不会解释 OnClick。周围的工作

isMove = false;
case MotionEvent.ACTION_DOWN:
//Your stuff
isMove = false;
case MotionEvent.ACTION_UP:
if (!isMove || (Xdiff < 10 && Ydiff < 10 ) {
view.performClick; //The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little
even when you just click it   
}
case MotionEvent.ACTION_MOVE:
isMove = true;
于 2014-09-24T18:06:58.450 回答
6

我试图在我的项目中应用@Biraj 解决方案但它没有用 - 我注意到扩展SimpleOnGestureListener不仅应该覆盖onSingleTapConfirmed方法,而且应该覆盖onDown。由于文档

如果你从 onDown() 返回 false,就像 GestureDetector.SimpleOnGestureListener 默认做的那样,系统假设你想忽略手势的其余部分,并且 GestureDetector.OnGestureListener 的其他方法永远不会被调用

下面是复杂的解决方案:

public class MainActivity extends Activity {
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetectorCompat(this, new SingleTapConfirm());
        ImageButton imageButton = (ImageButton) findViewById(R.id.img);

        imageButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (gestureDetector.onTouchEvent(arg1)) {
                    // single tap
                    return true;
                } else {
                    // your code for move and drag
                }

                return false;
            }
        });

    }

    private class SingleTapConfirm extends SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            /*it needs to return true if we don't want 
            to ignore rest of the gestures*/
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            return true;
        }
    }

}

我想,这种行为可能是由不同的 GestureDetectorCompat 引起的,但我会遵循文档,我将使用第二个:

您应该尽可能使用支持库类以提供与运行 Android 1.6 及更高版本的设备的兼容性

于 2016-02-17T15:41:11.920 回答
5

MainActivity代码中。

public class OnSwipeTouchListener_imp extends AppCompatActivity {

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

    ImageView view = (ImageView)findViewById(R.id.view);

    view.setOnTouchListener(new OnSwipeTouchListener(OnSwipeTouchListener_imp.this)
    {
        @Override
        public void onClick()
        {
            super.onClick(); // your on click here              
            Toast.makeText(getApplicationContext(),"onClick",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDoubleClick()
        {
            super.onDoubleClick(); // your on onDoubleClick here               
        }

        @Override
        public void onLongClick()
        {
            super.onLongClick(); // your on onLongClick here                
        }

        @Override
        public void onSwipeUp() {
            super.onSwipeUp(); // your swipe up here                
        }

        @Override
        public void onSwipeDown() {
            super.onSwipeDown();  // your swipe down here.

        }

        @Override
        public void onSwipeLeft() {
            super.onSwipeLeft(); // your swipe left here.                
            Toast.makeText(getApplicationContext(),"onSwipeLeft",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSwipeRight() {
            super.onSwipeRight(); // your swipe right here.                
            Toast.makeText(getApplicationContext(),"onSwipeRight",Toast.LENGTH_SHORT).show();
        }
    });
}
}

然后创建一个OnSwipeTouchListenerjava类。

public class OnSwipeTouchListener implements View.OnTouchListener {

private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
    gestureDetector = new GestureDetector(c, new GestureListener());
}

public boolean onTouch(final View view, final MotionEvent motionEvent) {
    return gestureDetector.onTouchEvent(motionEvent);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

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

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

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

    // Determines the fling velocity and then fires the appropriate swipe event accordingly
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
                {
                    if (diffX > 0)
                    {
                        onSwipeRight(); // Right swipe
                    } else {
                        onSwipeLeft();  // Left swipe
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown(); // Down swipe
                    } else {
                        onSwipeUp(); // Up swipe
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}

public void onClick() {
}

public void onDoubleClick() {
}

public void onLongClick() {
}
}

希望这能照亮你:)

于 2016-12-05T04:27:15.703 回答
2

也许您可以使用布尔值。

假设 Boolean isMoving = false;

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

    case MotionEvent.ACTION_MOVE:
                 isMoving = true;

          // implement your move codes
    break;

    case MotionEvent.ACTION_UP:
               isMoving = false;
    break;

default:
        break;
    }

然后在您的 onclick 方法中检查布尔值。如果为假,则执行点击动作,如果为真……不动作。

public void onClick(View arg0) {

    switch (arg0.getId()) {

    case R.id.imagebutton:
        if(!isMoving) {
                   //code for on click here
                }
    default:
        break;
    }
}
于 2013-10-23T10:15:59.613 回答
1

您可以通过获取 x,y 值的差异来区分触摸、单击和滑动,例如:

var prevY = 0
var prevX = 0
    controls.setOnTouchListener { v, event ->

                when (event?.actionMasked) {

                    MotionEvent.ACTION_DOWN -> {
                        prevY = event.rawY.toInt()
                        prevX = event.rawX.toInt()

                    }

                    MotionEvent.ACTION_UP->{
                        Log.d("Controls","Action up")
                        var y = event.rawY.toInt()
                        var x = event.rawX.toInt()
                        val diffY = Math.abs(prevY - y)
                        val diffX = Math.abs(prevX - x)
                        if(diffX in 0..10 && diffY in 0..10){
                          // its a touch
                        }
                         //check diffY if negative, is a swipe down, else swipe up
                      //check diffX if negative, its a swipe right, else swipe left
                    }
                }
                true
            }
于 2019-07-08T08:48:08.693 回答
1

希望我不会太晚,但你可以通过计时来实现这一点。单击只需不到一秒钟,因此请记住这一点....

    long prev=0;
    long current = 0;
    long dif =0; 
   public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:

                    prev = System.currentTimeMillis() / 1000;

                    break;
                case MotionEvent.ACTION_UP:

                    current = System.currentTimeMillis() / 1000;
                    dif = current - prev;
                    if (dif == 0) {
                        //Perform Your Click Action
                    }
                    break;
                 }
           }

希望它可以帮助某人

于 2019-04-13T00:03:37.430 回答
0

只需在触发 OnTouchListener 时使用布尔字段并将其设置为真值。之后,当 OnClickListener 想要触发时,您将检查布尔字段,如果为 true,则不要在您的 onClickListener 中执行任何操作。

    private blnTouch = false;

private OnTouchListener btnOnTouchListener = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction()==MotionEvent.ACTION_DOWN){
        blnOnTouch = true;
    }
    if (event.getAction()==MotionEvent.ACTION_UP){
        blnOnTouch = false;
    }
               }
 };

private OnClickListener btnOnClickListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        if (blnOnTouch){
            // Do Your OnClickJob Here without touch and move
        }

    }
};
于 2013-10-23T10:21:33.543 回答
0

点击监听事件;如果您的组件范围内的 actiondown 和 action up 比调用 onclicklistener。因此,onclick 事件通过触摸检测激活。如果您的组件开始位置上的动作向上和向下动作,您只能设置 onTouchListener 并接收点击事件。

于 2013-10-23T10:13:59.050 回答
0

声明一个私有变量,例如:boolean hasMoved = false;

当图像按钮开始移动时,设置hasMoved = true

仅在您的OnClickListener执行代码if(!hasMoved)中 - 意味着仅在按钮未移动时才执行点击功能。hasMoved = false;之后设置

于 2013-10-23T10:14:37.310 回答
0
Perfect example for Both touch n tap both    
private void touchNtap() {
            GestureDetector gestureDetector = new GestureDetector(itemView.getContext()
                    , new SingleTapConfirm());


            imageView.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (gestureDetector.onTouchEvent(event)) {
                        // program for click events
                        gestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
                            @Override
                            public boolean onSingleTapConfirmed(MotionEvent e) {
                                Log.d(TAG, "onSingleTapConfirmed() returned: " + true);
                                return false;
                            }

                            @Override
                            public boolean onDoubleTap(MotionEvent e) {
                                Log.d(TAG, "onDoubleTap() returned: " + true);
                                return false;
                            }

                            @Override
                            public boolean onDoubleTapEvent(MotionEvent e) {
                                Log.d(TAG, "onDoubleTapEvent() returned: " + true);
                                return false;
                            }
                        });
                        return true;
                    }else {
                        //program for touch events
                    }
                    return false;
                }
            });
        }
Create a Inner class 

    private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {

            @Override
            public boolean onSingleTapUp(MotionEvent event) {
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                return true;
            }
        }
于 2020-04-12T09:34:17.967 回答