如何使用Ontouch和Onclick作为ImageButton?

时间:2013-10-23 10:08:42

标签: android

在我的应用中,我想要发生两件事。

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

    我使用OnTouchListener()来做这件事并且工作正常。

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

    我使用了OnClickListener(),它也可以正常工作。

  3. 所以,这是我的问题。每当我移动ImageButton OnTouchListener时,ImageButton移动,OnClickListener也会在我释放按钮移动时触发。

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

12 个答案:

答案 0 :(得分:118)

试试这个,它可以帮到你

无需设置onClick()方法onTouch()将处理这两种情况。

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;
        }
    }

}

答案 1 :(得分:30)

Click ListenerDoubleClick ListenerOnLongPress ListenerSwipe LeftSwipe RightSwipe UpSwipe Down在{{} 1}}你需要View。即,

setOnTouchListener

为此,您需要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

答案 2 :(得分: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;

答案 3 :(得分:5)

我已经尝试在我的项目中应用@Biraj解决方案而且它没有用 - 我注意到SimpleOnGestureListener的扩展不应该只覆盖onSingleTapConfirmed方法,但onDown也是如此。由于documentation

  

如果你从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造成的,但我会跟随documentation而我将使用第二个:

  

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

答案 4 :(得分:3)

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();
        }
    });
}
}

然后创建一个OnSwipeTouchListener java类。

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() {
}
}

希望这点亮你:)

答案 5 :(得分: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方法中检查布尔值。如果为false,则执行click操作,如果为true ...不执行。

public void onClick(View arg0) {

    switch (arg0.getId()) {

    case R.id.imagebutton:
        if(!isMoving) {
                   //code for on click here
                }
    default:
        break;
    }
}

答案 6 :(得分:0)

Clieck Listener事件;如果在组件边界中的actiondown和action up,则调用onclicklistener。因此,通过触摸检测激活oncl​​ick事件。如果组件中的操作向上和操作停止,则只能设置onTouchListener并接收单击事件。

答案 7 :(得分:0)

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

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

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

答案 8 :(得分:0)

只需使用布尔字段,并在触发OnTouchListener时将其设置为true值。之后,当OnClickListener想要触发时,你将检查boolean字段,如果为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
        }

    }
};

答案 9 :(得分:0)

希望我还不算太晚,但是您可以通过时间计数器来实现。 点击只需不到一秒钟,因此请牢记。...

    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;
                 }
           }

希望它可以帮助某人

答案 10 :(得分:0)

您可以通过获取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
            }

答案 11 :(得分: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;
            }
        }