Learn about event distribution details of Android View

preface:

According to the custom MyButton button button, we set the listening event for this button, and understand the event distribution through the output log.

1, MyButton class

public class MyButton extends AppCompatButton {
    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //Handling the distribution of touch events starts with dispatchTouchEvent
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.e("dispatchTouchEvent", "---dispatchTouchEvent--- " );
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.e("MyButton", "---onTouchEvent---" );
                break;
        }
        return super.onTouchEvent(event);
    }
}

After writing the xml layout file, the code will not be given.
Finally, set the listening event of the button in EventActivity2.

public class EventActivity2 extends AppCompatActivity {
    private static final String TAG = "EventActivity2";
    private MyButton my_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event2);
        my_btn = findViewById(R.id.my_btn);
        my_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.e(TAG, "onTouch " );
                        break;
                }
                return false;
            }
        });
        my_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: " );
            }
        });
    }
}

After we click the button, the output log is:

analysis:
The distribution of touch events starts with dispatchTouchEvent(), so handle the method dispatchTouchEvent() first.
After that, start to process the listening event of the button and press the action.
After execution, start to execute the onTouchEvent () callback method in the MyButton control.
After that, we execute dispatchTouchEvent(), because when the user lifts up, we also start from the entrance of touch event dispatchTouchEvent(), and then we execute onClick click event.

So the distribution of our View events is from
Dispatchtouchevent – > setontouchlistener – > ontouchevent (callback method of touch event).
Where onClick and onLongClick are the processing from onTouchEvent().

Note: when we press the button with our fingers, execute action_ The action of down will be delayed for 100 milliseconds to check whether the action has been executed_ If the up phase is not executed, it will send another delay of 400 milliseconds to check whether the action has been executed_ Up this link.
That is, press and hold the control for at least 500 milliseconds for a long time. If it is less than 500 milliseconds, it is a click event.
Premise: when we press it, we will act on the screen_ Move to slide. If this control is slid out, our onClick and onLongClick will be removed by remove.

The return value of onLongClickListener for long press time is boolean. If the return value is true, it indicates that this event has been consumed and will not execute onClick click event again. If the return value is false, it indicates that this event has not been consumed and will continue to execute click event.

Keywords: Android

Added by neoform on Tue, 28 Dec 2021 00:39:13 +0200