Android中点击和点按的区别

前端之家收集整理的这篇文章主要介绍了Android中点击和点按的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
[GestureDetector.OnGestureListener] [1]类的方法有[onSingleTapUp(MotionEvent)] [2]:

Notified When a tap Occurs with the up MotionEvent que triggered it.

这种方法具有相同的onclick功能?我可以使用此方法从onclick方法实现我想要的相同行为吗?

解决方法

我做了一些测试,似乎答案是肯定的,你可以使用onSingleTapUp而不是onclick. onSingleTapUp方法可以很好地区分双击或长按的简单点击.我的测试:
public class MainFragmentScrollerController
{
    private MainFragment mFragment;
    private GestureDetectorCompat mGestureDetector;

    public MainFragmentScrollerController(MainFragment fragment)
    {
        mFragment = fragment;
        mGestureDetector = new GestureDetectorCompat(mFragment.getActivity(),new GestureListener());
    }

    public void setGestureListener()
    {
        mFragment.myView.setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v,MotionEvent event)
            {
                boolean gestureResult = mGestureDetector.onTouchEvent(event);
                Log.i("GestureListener","gestureResult = " + gestureResult);
                return gestureResult;
            }
        });
    }

    public class GestureListener extends SimpleOnGestureListener
    {
        private final String TAG = "GestureListener";
        @Override
        public boolean onDown(MotionEvent event)
        {
            Log.d(TAG,"onDown: " + event.toString()); 
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1,MotionEvent event2,float velocityX,float velocityY)
        {
            Log.d(TAG,"onFling: " + event1.toString()+event2.toString());
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent event1,float distanceX,float distanceY)
        {
            Log.d(TAG,"onScroll: " + event1.toString()+event2.toString());
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent event)
        {
            Log.d(TAG,"onSingleTapUp: " + event.toString());
            return true;
        }

        @Override
        public void onLongPress(MotionEvent event)
        {
            Log.d(TAG,"onLongPress: " + event.toString());
        }
    }
}

简单点击的结果:

09-04 14:20:58.040: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN,id[0]=0,x[0]=188.71875,y[0]=26.0625,toolType[0]=TOOL_TYPE_FINGER,buttonState=0,MetaState=0,flags=0x0,edgeFlags=0x0,pointerCount=1,historySize=0,eventTime=34463725,downTime=34463725,deviceId=1,source=0x1002 }
09-04 14:20:58.040: I/GestureListener(17689): gestureResult = true
09-04 14:20:58.100: I/GestureListener(17689): gestureResult = false
09-04 14:20:58.130: D/GestureListener(17689): onSingleTapUp: MotionEvent { action=ACTION_UP,eventTime=34463811,source=0x1002 }
09-04 14:20:58.130: I/GestureListener(17689): gestureResult = true

长按结果:

09-04 14:22:12.520: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN,x[0]=179.22656,y[0]=41.0625,eventTime=34538199,downTime=34538199,source=0x1002 }
09-04 14:22:12.520: I/GestureListener(17689): gestureResult = true
09-04 14:22:12.560: I/GestureListener(17689): gestureResult = false
09-04 14:22:12.930: I/GestureListener(17689): gestureResult = false
09-04 14:22:12.950: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.010: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.100: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.130: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.150: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.180: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.200: D/GestureListener(17689): onLongPress: MotionEvent { action=ACTION_DOWN,source=0x1002 }
09-04 14:22:13.230: I/GestureListener(17689): gestureResult = false

投掷结果:

09-04 14:24:09.650: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN,x[0]=332.15625,y[0]=2.625,eventTime=34655337,downTime=34655337,source=0x1002 }
09-04 14:24:09.650: I/GestureListener(17689): gestureResult = true
09-04 14:24:09.690: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN,source=0x1002 }MotionEvent { action=ACTION_MOVE,x[0]=331.9604,y[0]=-72.36835,historySize=2,eventTime=34655368,source=0x1002 }
09-04 14:24:09.690: I/GestureListener(17689): gestureResult = true
09-04 14:24:09.710: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN,x[0]=337.91425,y[0]=-124.40411,eventTime=34655385,source=0x1002 }
09-04 14:24:09.710: I/GestureListener(17689): gestureResult = true
09-04 14:24:09.730: D/GestureListener(17689): onFling: MotionEvent { action=ACTION_DOWN,source=0x1002 }MotionEvent { action=ACTION_UP,eventTime=34655414,source=0x1002 }
09-04 14:24:09.730: I/GestureListener(17689): gestureResult = true
原文链接:https://www.f2er.com/android/314964.html

猜你在找的Android相关文章