A widget in Android can register multiple events at the same time, such as a Button. It can listen to touch events, click events, and hold events at the same time. Different operations can be performed under different circumstances, so how is it done?
First, the onTouch event occurs first. The returned value of this event also determines whether a long-press event or a click event can occur. Touch screen operations consist of some basic events, such as down events, up events, move events, and scroll events. After testing, the execution sequence of each event is as follows:
Button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "Click Event", Toast. LENGTH_LONG ). show () ;}}); button. setOnTouchListener (new View. onTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub // Toast. makeText (MainActivity. this, "Touch event", Toast. LENGTH_LONG ). show (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: Toast. makeText (MainActivity. this, "down event", Toast. LENGTH_SHORT ). show (); break; case MotionEvent. ACTION_UP: Toast. makeText (MainActivity. this, "up event", Toast. LENGTH_SHORT ). show (); break; default: break;} return true ;}}); button. setOnLongClickListener (new View. onLongClickListener () {@ Override public boolean onLongClick (View v) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "Long Click Event", Toast. LENGTH_LONG ). show (); return false ;}});}
1. By default, both onTouch and onLongClick return false. false means that the event is not consumed and can be passed on. Possible operations:
(1) down -- up -- longClick -- click; touch event -- long press event -- click Event
(2) down -- up -- click; touch event -- click Event
The reason for these two cases is that the duration of your finger's stay is long. If you press it for a long time, go (1). If you click it for a short time, go (2 ).
2. If the onTouch event returns true, the event is consumed. If the event is not transmitted, the click or long-press event does not occur. Operation:
Down -- up
3. onTouch returns false, and onLongClick returns true. Operation:
(1) down -- up -- longClick touch event -- long press event
(2) down -- up -- click touch event -- click Event
Through comparison between 1 and 3, it is found that if the onLongClick event return value is not manually changed, a long-press event will definitely cause a click event.
Of course, this is just a superficial understanding of the onTouch event. If you want to learn more, please refer to the following article: Click to open the link