Summary of Android event handling methods – callback based event handling

Time:2021-11-30

1、 Event handling methods in Android

Event handling: respond to user UI actions and improve application interactivity

1. Event handling mechanism based on listening

2. Callback based event handling mechanism

3. Handler message processing

Previously, we have introduced the summary of Android event handling methods – based on listening. Here we summarize the summary of Android event handling methods – based on callback

2、 Detailed explanation of callback based event handling mechanism

1. Callback event processing principle

Listening event processing is separated from event source and event listener

The callback based event processing UI component is not only an event source, but also an event listener. It processes the corresponding events through the relevant callback methods of the component

2. Callback event application steps

I. custom view class, inherited from the required view UI class. Ex: Custom MyButton button button class extends basic button class

II. Replication callback function. ex:public boolean onTouchEvent(MotionEvent event)

Each event callback method will return a Boolean value, ①. If true is returned, it means that the event has been processed and will not continue to spread outward; ② if false is returned, it means that the event continues to spread outward

The android.view.view class provides many callback methods. Ex: onkeydown monitors the keyboard press…, please refer to the API documentation for details

3. Callback event application example

Demo: after clicking the button, toast pops up the event information of the touched button

Customize the view class MyButton and override the event callback method

package com.yihui.ui;
 
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.Toast;
 
public class MyButton extends Button {
 
	private static final String TAG = "EventCallBack";
 
	public MyButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	/*Override the callback method of ontouchevent touch event*/
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log. I (tag, "I'm MyButton, you touched me:" + event. Getaction());
		Toast. Maketext (getcontext(), "I'm MyButton, you touched me:" + event. Getaction(), 0). Show();
		return false; // Returning false indicates that the event continues to spread to the outer layer (i.e. the parent container)
	}
}

Layout file XML: activity_ main.xml

Activiy also replicates the event callback method to test the spread of events

package com.yihui.eventhandler;
 
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    private static final String TAG = "EventCallBack";
 
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	
    	Log. I (tag, "I'm mainactivity, found touch event:" + event. Getaction());
    	Toast. Maketext (mainactivity. This, "I'm mainactivity, found touch event!:" + event. Getaction(), 0). Show();
    	return true;
    }
}

Test effect:

image