Android:跨活动捕获 MotionEvent

Eri*_*hen 5 android motion ontouchlistener touch-event android-activity

我有两个活动 A 和 B。我希望在 A 中捕获一个触摸事件 MotionEvent.ACTION_DOWN,同时仍然按住,启动 B,然后在 B 中捕获释放事件 MotionEvent.ACTION_UP。

在 A 中有一个 View v,它有一个带有以下回调的 OnTouchListener:

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        startActivity(new Intent(A.this, B.class));
        break;
    case MotionEvent.ACTION_UP:
        // not called
        break;
    }
    // false doesn't work either
    return true;
}
Run Code Online (Sandbox Code Playgroud)

在 B 中,有一个重叠的 View v2(在原始 v 之上),具有相同类型的 OnTouchListener,但是当活动开始时,B 的“onTouch”不会被调用,除非我移动手指(重新生成触摸事件)。

简而言之,我正在做一个应用程序,当按住屏幕时会出现一个新的活动,当我松开手指时会结束。

是否不可能将 MotionEvent.ACTION_DOWNed 状态从一个视图转移到另一个视图?或者新活动 B 是否清除任何当前仅适用于 A 的“屏幕触摸侦听器”,因为它是在那里启动的?

感谢您对如何将这些 MotionEvents 分派到活动和/或解决我的问题的任何解决方案/黑客的任何解释。

A.Q*_*oga 1

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        startActivity(new Intent(A.this, B.class));
        break;
    case MotionEvent.ACTION_UP:

          // Obtain MotionEvent object
          long downTime = SystemClock.uptimeMillis();
          long eventTime = SystemClock.uptimeMillis() + 100;
          float x = 0.0f;
          float y = 0.0f;
          // List of meta states found here:               developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
          int metaState = 0;
          MotionEvent motionEvent = MotionEvent.obtain(
             downTime, 
             eventTime, 
             MotionEvent.ACTION_UP, 
             x, 
             y, 
             metaState
            );

            // Dispatch touch event to activity (make B static or get the activity var some other way)

            B.OnTouchEvent(motionEvent);
       break;
    }
    // false doesn't work either
    return true;
}
Run Code Online (Sandbox Code Playgroud)

在 B Activity 中重写 OnTouchEvent (使 B 实现 OnTouchListener),如下所示:

@Override
public bool OnTouchEvent( MotionEvent e )
{
    return someview.OnTouchEvent( e );
}
Run Code Online (Sandbox Code Playgroud)

请记住, someview 必须是一个视图才能捕获 ontouchevent ,因为活动并不真正知道如何处理它。