是否可以检测活动中的所有触摸事件并捕获它,然后将传递事件的回传传递到另一个视图?
例如:
按钮1和按钮2.当按下按钮1时,我想捕获该触摸/点击事件,并自动将该触摸事件传递到按钮2,基本上单击/按下可以获得点击生成,同样的点击被传递到第二个按钮自动.
解决方法
首先看看这个API描述.
boolean
android.app.Activity.dispatchTouchEvent(MotionEvent
ev)public boolean dispatchTouchEvent
(MotionEvent ev) Since: API Level 1
Called to process touch screen events.
You can override this to intercept all
touch screen events before they are
dispatched to the window. Be sure to
call this implementation for touch
screen events that should be handled
normally.Parameters ev The touch screen event.
Returns boolean Return true if this
event was consumed.
你可以看到,你可以拦截所有的触摸事件.
@Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub super.dispatchTouchEvent(ev); if(btn1.onTouchEvent(ev)){ return btn2.onTouchEvent(ev); }else{ return false; } }
这些代码是你想要的.