Android basic events
I. Basic Android events
1. onTouchEvent touch event (GestureDetector gesture operation class)
2. Press the onKeyDown keyboard
3. onKeyLongPress long press event (Keyboard press event long)
4. Release the onKeyUp keyboard
5. Repeat the onKeyMultiple keyboard
6. onBackPressed returned event
NOTE: You can use the KeyEvent class to obtain static member variables (for example, KeyEvent. KEYCODE_BACK: listens to the Return key to determine the keyCode and KeyEvent. the KEYCODE_BACK value is equal); the screen event uses the event. getAction () obtains the action and MotionEvent. ACTION_DOWN and MotionEvent. ACTION_UP: determines whether to press or release, obtains the X and Y coordinates when pressing, obtains the X and Y coordinates when releasing, and compares them to determine which direction to slide, for complex gestures, you must use the GestureDetector class (this class is recommended for personal use ).
Ii. What is the difference between onTouchEvent and onTouch? (Others)
1. onTouch usage:
Public class TActivity extends Activity implements OnTouchListener {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. main); TextView TV = (TextView) findViewById (R. id. TV); TV. setOnTouchListener (this);} // use of onTouch @ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event. getAction () {case MotionEvent. ACTION_DOWN: // press break; case MotionEvent. ACTION_UP: // release break;} return false ;}}
2. onTouchEvent usage:
public class TActivity extends Activity{@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn super.onTouchEvent(event);}}
3. Android-related source code:
public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { onUserInteraction(); } if (getWindow().superDispatchTouchEvent(ev)) { return true; } return onTouchEvent(ev);}
Conclusion: we can see that the onTouchListener interface has a higher priority than onTouchEvent. If the onTouch method in onTouchListener returns true, the onTouchEvent will not receive the message. Because onTouchEvent is used to implement the worker mclick of the Button, if onTouchEvent is not called, The Click Event of the Button cannot respond.
Iii. Analysis of GestureDetector gesture operations
The GestureDetector class provides two external interfaces: OnGestureListener, OnDoubleTapListener, and an internal class SimpleOnGestureListener.
1. Implementation interface: OnGestureListener
A. triggered when onDown () Touch down
B. If onShowPress () Touch is not triggered when it is not sliding (compared with onDown, onLongPress), it must be triggered immediately as long as the Touch is down. After Touchdown, onShowPress is triggered before onLongPress after a while without sliding. Therefore, it does not slide after Touchdown. onDown-> onShowPress-> onLongPress is triggered in this order.
C. onSingleTapUp () the above two functions are triggered when the touch is down without moving (onScroll) or long-pressed (onLongPress.
Click very fast (do not slide) Touchup: onDown-> onSingleTapUp-> onSingleTapConfirmed
Click a slower (do not slide) Touchup: onDown-> onShowPress-> onSingleTapUp-> onSingleTapConfirmed
D. onScroll () drag the event on the screen. Whether you drag the view by hand or scroll by throwing the action, it will be triggered multiple times. This method will trigger the throw when the ACTION_MOVE action occurs: After the finger touches the screen, slide a little and then immediately release onDown-> onScroll-> ......... -> OnFling: Drag onDown-> onScroll-> onFiling
E. onLongPress () Touch is triggered when Touch is down without moving
F. onFling () slide gesture event; after a sliding distance is reached, the parameter will be triggered only when ACTION_UP: e1 1st ACTION_DOWN MotionEvent and only one; e2 the last ACTION_MOVE MotionEvent; the moving speed on the X axis of velocityX, pixel/second; the moving speed on the Y axis of velocityY, pixel/second. trigger condition: the coordinate displacement of the X axis is greater than FLING_MIN_DISTANCE, and the moving speed is greater than FLING_MIN_VELOCITY pixels/second.
2. Implementation interface: OnDoubleTapListener
A. onSingleTapConfirmed () is used to determine that the click is SingleTap rather than DoubleTap. If you click twice consecutively, It is a DoubleTap gesture. If you click only once, after waiting for a while, the system determines that the second click is SingleTap instead of DoubleTap, and then triggers the SingleTapConfirmed event. This method is different from onSingleTapUp, which is triggered when GestureDetector is sure that the user is behind the screen for the first time without following the second touch screen.
Click very fast (do not slide) Touchup: onDown-> onSingleTapUp-> onSingleTapConfirmed
Click a slower (do not slide) Touchup: onDown-> onShowPress-> onSingleTapUp-> onSingleTapConfirmed
B. triggered when the next Touch is down on the onDoubleTap () double-click
C. onDoubleTapEvent () notifies the events in the DoubleTap gesture, including the down, up, and move events (this refers to the events that occur between double-clicking, for example, double-clicking in the same place will generate the DoubleTap gesture, in the DoubleTap gesture, the down and up events will occur, which are notified by the function); in the second double-click, the Touch down and up events will be triggered. e. getAction () is differentiated.
3. The SimpleOnGestureListener class is a class provided by GestureDetector for us to easily respond to different gestures. This class implements the above two interfaces, and we can override any method through inheritance.
NOTE: Each method has an instance of the MotionEvent class as a parameter. We can use this instance and class operation.
These are the basis for beginners to learn and learn more deeply. You are welcome to read and learn.