In the past few days, some Android-related touch events have also been related to onClick. Now I want to write down:
LinearLayout respectively sets onTouchListener, onClickListener, onLongClickListener and onTouchEvent callback.
1. After the screen is touched, the basic execution process is as follows:
OnTouch, action = 0
OnTouchEvent, action = 0
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 1
OnTouchEvent, action = 1
OnClick
That is to say, onTouchListener is triggered first, and then onTouchEvent callback. onClickListener is triggered only when the last up event occurs and is processed by onTouchEvent.
2. Remove super. onTouchEvent from the onTouchEvent callback and return true directly. The process is as follows:
OnTouch, action = 0
OnTouchEvent, action = 0
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 1
OnTouchEvent, action = 1
We can see that onClickListener can never be triggered. It also shows that onClick is triggered in onTouchEvent callback.
3. Long press the screen. The process is as follows:
OnTouch, action = 0
OnTouchEvent, action = 0
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 2
OnTouchEvent, action = 2
OnLongClick
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 1
OnTouchEvent, action = 1
OnClick
When you press for a long time, the onLongClick response is triggered without being up, but the onClick response is triggered later.
4. If true is returned in onLongClick, the process is as follows, that is, only onLongClick is triggered in the middle, and then the system will continue to respond to touch, but The onClick will not be triggered when it is up.
OnTouch, action = 0
OnTouchEvent, action = 0
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 2
OnTouchEvent, action = 2
OnLongClick
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 2
OnTouchEvent, action = 2
OnTouch, action = 1
OnTouchEvent, action = 1
The test code is as follows:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FrameLayout frame = (FrameLayout)findViewById(R.id.container); LinearLayoutTest line = new LinearLayoutTest(this); line.setOnClickListener(new ClickListener()); line.setOnLongClickListener(new LongClickListener()); line.setOnTouchListener(new TouchListener()); line.setLongClickable(true); frame.addView(line); } public class ClickListener implements OnClickListener { @Override public void onClick(View v) { Log.e("test","onClick"); } } public class LongClickListener implements OnLongClickListener{ @Override public boolean onLongClick(View v) { Log.e("test","onLongClick"); return true; } } public class TouchListener implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { Log.e("test","onTouch,action="+event.getAction()); return false; } } }
public class LinearLayoutTest extends LinearLayout{ public LinearLayoutTest(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("test","onTouchEvent,action="+event.getAction()); return super.onTouchEvent(event); }}