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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值