I want to find out any event when user t开发者_如何学Pythonouches on any screen of an android. I find out touch event for particular activity but not for all screen, please give me a solution.
try this code
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
The problem you might have when using the onTouchEvent()
is that because of your view hierarchy where you also have other views on top of your activity, touches on views that override this event (buttons, edittext etc) will not go down to you activity anymore and you will not receive them.
You should use dispatchTouchEvent()
instead as this propagates the other way around, from the activity to your other views and always gets called.
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// Your code here
return super.dispatchTouchEvent(ev);
}
android touch event is dispatched level by level, start from activity to the viewgroup and the view. such as Activity--RelativeLayout--Button, there is a lot of override methods can control the touch events. This may help you
I don't think that's possible.
You could use an extra class which recieves all touch-events from all your activities. For that you'll have to implement the passing by in every activity.
publich class TouchStore(){
public void recieveTouchEvent(MotionEvent event){
//Handle your events
}
}
public boolean onTouchEvent(MotionEvent event) {
touchStore.recieveTouchEvent(event);
}
精彩评论