【问题标题】:How can I get onClick/onLongClick events to work for a VideoView?如何让 onClick/onLongClick 事件为 VideoView 工作?
【发布时间】:2012-09-22 04:19:59
【问题描述】:

我有一个 VideoView,我想在短按时调用 fooMethod(VideoView vv) 的方法,但如果长按,则显示/隐藏视频控件。但是,VideoView 似乎没有 OnClick 或 OnLongClick 事件。

我已经实现了 onTouchListener,但我似乎只能从中获取 DOWN 和 UP 事件;似乎不支持检测点击的长度。

在onTouchListener中有区分长按和短按的好方法吗?

【问题讨论】:

  • 您可以自己执行此操作,在触地时将当前毫秒保存到变量中,并在触地时计算现在(当前毫秒)与触地时的变量之间的差异。
  • 多点触控设备呢?我把那个变量放在哪里?活动的全局?静态方法?谢谢
  • 我认为如果你想要的话,你可以禁用多点触控。此外,如果您愿意,您可以将其保存为整个活动的实例变量,这取决于您。

标签: android


【解决方案1】:

尝试添加

<uses-features android:name="multi-touch" />

在您的 Manifest.xml 中

【讨论】:

    【解决方案2】:

    这有点旧,但可以帮助将来遇到同样问题的任何人。

    看起来 VideoView 将 onClick/onLongClick 用于其他功能(没有对其进行足够深入的研究),因此另一种方法是使用 GestureDetectorCompat 手动完成(兼容,因此它适用于旧版本)。

    GestureDetectorCompat

    GestureDetectorCompat gestureDetectorCompat =  new GestureDetectorCompat(context, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                  /* Always return true, to indicate that the gestureDetectorCompat
                   * consumed the touch and can continue to the  
                   * next gestures(long, single, etc..)
                   */
                return true;
            }
    
            @Override
            public void onShowPress(MotionEvent motionEvent) {
    
            }
    
            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return false;
            }
    
            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
    
            @Override
            public void onLongPress(MotionEvent motionEvent) {
                Log.i("SOME_TAG", "Longpress detected");
            }
    
            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
        });
    

    您要实现 onClick/onLongClick 的视图。

    查看

    view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // Return the result from GestureDetectorCompat so the view
                // knows if the touch was consumes or not
                return gestureDetectorCompat.onTouchEvent(motionEvent);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多