In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the difference between touch event and click event in Android". In daily operation, I believe that many people have doubts about the difference between touch event and click event in Android. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between touch event and click event in Android". Next, please follow the editor to study!
1. OnTouchEvent
The three most common events to be handled in onTouchEvent are: ACTION_DOWN, ACTION_MOVE, and ACTION_UP.
These three events identify the most basic operation of the user touching the screen, and the meaning is clear. Although people use them every day, please note that the importance of the ACTION_DOWN event as the starting event is greater than that of ACTION_MOVE and ACTION_UP. If there is an ACTION_MOVE or ACTION_UP, then ACTION_DOWN must have happened.
Some interaction mechanisms based on this understanding of different importance can be seen in the source code of Android, and it is also explicitly mentioned in SDK. For example, in ViewGroup's onInterceptTouchEvent method, if true is returned in an ACTION_DOWN event, then subsequent events will be sent directly to onTouchEvent instead of continuing to send to onInterceptTouchEvent.
2. OnClick, onLongClick and onTouchEvent
I once read a post that mentioned that if you deal with onTouchEvent in View, you don't have to deal with onClick anymore, because Android will only trigger one of the methods. This understanding is not quite correct, for a certain view, the user completed a touch operation, obviously the signal from the sensor is finger press and lift two operations, we can understand as a Click, can also be understood as an ACTION_DOWN and ACTION_UP, so how does Android understand and deal with it?
In Android, the triggers of onClick and onLongClick are related to ACTION_DOWN and ACTION_UP. In timing, if we overwrite onClick, onLongClick and onTouchEvent in a View at the same time, onTouchEvent captures ACTION_DOWN and ACTION_UP events, followed by onClick or onLongClick. The main logic is implemented in the onTouchEvent method in View.java:
Case MotionEvent.ACTION_DOWN:
MPrivateFlags | = PRESSED
RefreshDrawableState ()
If ((mViewFlags & LONG_CLICKABLE) = = LONG_CLICKABLE) {
PostCheckForLongClick ()
}
Break
Case MotionEvent.ACTION_UP:
If ((mPrivateFlags & PRESSED)! = 0) {
Boolean focusTaken = false
If (isFocusable () & & isFocusableInTouchMode () & &! isFocused ()) {
FocusTaken = requestFocus ()
}
If (! mHasPerformedLongPress) {
If (mPendingCheckForLongPress! = null) {
RemoveCallbacks (mPendingCheckForLongPress)
}
If (! focusTaken) {
PerformClick ()
}
}
...
Break
As you can see, the trigger of Click occurs after the system captures the ACTION_UP and is executed by performClick (). The onClick () method of the previously registered listener is called in performClick:
Public boolean performClick () {
...
If (mOnClickListener! = null) {
PlaySoundEffect (SoundEffectConstants.CLICK)
MOnClickListener.onClick (this)
Return true
}
Return false
}
The trigger of LongClick starts with ACTION_DOWN and is completed by the postCheckForLongClick () method:
Private void postCheckForLongClick () {mHasPerformedLongPress = false; if (mPendingCheckForLongPress = = null) {mPendingCheckForLongPress = new CheckForLongPress ();} mPendingCheckForLongPress.rememberWindowAttachCount (); postDelayed (mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout ());}
As you can see, after the ACTION_DOWN event is captured, the system starts to trigger a postDelayed operation, and the execution of the CheckForLongPress thread will be triggered when the delay time is 500ms on Eclair2.1:
Class CheckForLongPress implements Runnable {
...
Public void run () {
If (isPressed () & & (mParent! = null)
& & mOriginalWindowAttachCount = = mWindowAttachCount) {
If (performLongClick ()) {
MHasPerformedLongPress = true
}
}
}
...
}
If all the conditions are met, performLongClick () is executed in CheckForLongPress, where onLongClick () is called:
Public boolean performLongClick () {
...
If (mOnLongClickListener! = null) {
Handled = mOnLongClickListener.onLongClick (View.this)
}
...
}
You can see from the implementation that the onClick () and onLongClick () methods are captured by ACTION_DOWN and ACTION_UP events and finally determine whether to trigger them according to various circumstances, that is, if we listen or override the onClick (), onLongClick () and onTouchEvent () methods in an Activity or View at the same time, it does not mean that only one of them will occur.
Here is a Log of the basic timing in which the onClick is triggered:
04-05 05Partition 57 47.123: DEBUG/TSActivity (209): onTouch ACTION_DOWN
04-05 05PUR 57UR 47.263: DEBUG/TSActivity (209): onTouch ACTION_UP
04-05 05Partition 57 47.323: DEBUG/TSActivity: onClick
You can see that it happens in the order of ACTION_DOWN-> ACTION_UP-> onClick.
Here is a Log of the basic timing in which the onLongClick is triggered:
04.133: DEBUG/TSActivity (248): onTouch ACTION_DOWN
04.642: DEBUG/TSActivity (248A): onLongClick
04-05 06VOUR 05.083: DEBUG/TSActivity (248): onTouch ACTION_UP
As you can see, onLongClick is triggered after holding down for a certain period of time, and then ACTION_UP occurs when the hand is raised.
3. Can onClick and onLongClick happen at the same time?
To understand this problem, you only need to understand Android's so-called consume concept of event handling. A user's operation will be passed to different View controls and different listening methods of the same control. If any method that receives and handles the event returns true after processing, then the event will be completely processed, and other View or listening methods will no longer have a chance to deal with the event.
The occurrence of onLongClick is done by a separate thread, and before ACTION_UP, while the occurrence of onClick is after ACTION_UP, so it is possible for both onLongClick and onClick to occur in the same user touch operation. Isn't that incredible? Therefore, it is very important to say to the system in time that "I have completely handled (consumed) the user's operation". For example, if we are in the * * return true of the onLongClick () method, then there is no chance that the onClick event will be triggered.
The following Log is the basic timing of a touch operation in the case of the onLongClick () method return false:
04-05 06PUBG 0015 023: DEBUG/TSActivity (277): onTouch ACTION_DOWN
04-05 06 DEBUG/TSActivity 0015: 53.533: DEBUG/TSActivity (277): onLongClick
04-05 06PUR 0015 55.603: DEBUG/TSActivity (277): onTouch ACTION_UP
04-05 06Partition 0055.663: DEBUG/TSActivity (277): onClick
At this point, the study on "what is the difference between touch events and click events in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.