Andorid Long press event handling

When developing applications, long press is an essential type of events needing to handle in graphical user interface. For the customized view defined by yourself, you can handle it by implementingView.OnLongClickListener or implementing View.OnTouchEvent. For the common widgets, you can also implement these two interfaces and set them to widgets.

Now you must wanna know the differences between View.OnLongClickListener and View.OnTouchEvent when handling long press event:

1. View.OnLongClickListener  has only one method and will be called only once, which is much similar to View.OnClickListener. And the callback event is fired off after the action of long pressing, i.e. the onLongClick() is called when you release your fingers.

2. View.OnTouchEvent, however, is a little big complex. You have to recognize current actions and do proper responses, i.e. you have to get the actions from MotionEvent object and test what kind of action is and then do proper responses.

Generally speaking, you have to handle four types of actions:

     MotionEvent.ACTION_DOWN ---- user has pressed on your view

     MotionEvent.ACTION_MOVE ---- user is moving on your view

     MotionEvent.ACTION_UP ---- user has released his fingers

     MotionEvent.ACTION_CANCEL ---- user has aborted his actions, released his fingers, etc.

onTouch() is hard to implement, though, it is powerful and flexible enough to handle any event you want.View.OnLongClick(), for instance, can be implemented with onTouch. Firing offonLongClick() when getting MotionEvent.ACTION_UP in onTouch is eough to handle the long click event.

In all, if you want to do some separate tasks when long pressing, like pop up some menus, or pop up a notification or enter into another screen or activities, you should useView.OnLongClickListener.

But if you want to change some states or interact with other tasks, or need to know the details of user actions, you should implementView.OnTouchEvent. For instance, if you wanna control something should only happen when long pressing a button, you can use a flag which turn it on whenACTION_DOWN and turn it off when ACTION_UP.


你可能感兴趣的:(user,UP,action,callback,events,firing)