我有两个活动A和B。我想让一个触摸事件MotionEvent.ACTION_DOWN在A中被捕获,同时仍然按住,启动B,然后让发布事件MotionEvent.ACTION_UP在B中被捕获。
在A中,有一个具有以下回调的OnTouchListener的View:
@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;
}在B中,有一个与相同类型的v2重叠的视图OnTouchListener (在原始v上),但是当活动开始时,B的"onTouch“不会被称为,除非我移动手指(重新生成触摸事件)。
简单地说,我正在做一个应用程序,它会在按住屏幕时出现一个新的活动,并在我释放手指时完成。
难道不可能有一个MotionEvent.ACTION_DOWNed状态从一个视图转移到另一个视图吗?或者,新的活动B是否清除任何当前的“屏幕上的触摸侦听器”,因为它是在那里启动的?
感谢您对如何将这些MotionEvents分配到活动和/或任何解决方案/攻击我的问题的解释。
发布于 2011-10-06 21:47:13
@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;
}在B活动中,重写OnTouchEvent (make实现OnTouchListener)如下:
@Override
public bool OnTouchEvent( MotionEvent e )
{
return someview.OnTouchEvent( e );
}记住,某些视图必须是一个视图才能捕获ontouchevent事件,因为activitys并不真正知道如何处理它。
https://stackoverflow.com/questions/7680732
复制相似问题