Android callback-based event processing
The callback-based event processing model is simpler:
If the event listening mechanism is a delegated event processing mechanism, the callback mechanism is exactly the opposite: For the callback-based time processing model, the event sources and Event Listeners are unified, or the event listener disappears completely. When a user fires an event on a GUI component, the component's own method is responsible for handling the event.
Therefore, the callback listening mechanism only needs to inherit the component and rewrite the event processing method of the component class to implement
For Event Callback, Android provides some event processing callback methods for all GUI components. Take View as an example:
Boolean onKeyDown (int keyCode, KeyEvent event); this method is triggered when you press a key on this component.
Boolean onKeyShortcut (int keyCode, KeyEvent event); this method is triggered when a keyboard shortcut event occurs.
Boolean onKeyLongPress (int keyCode, KeyEvent event). This method is triggered when you press a key on the component.
Boolean onTouchEvent (int keyCode, KeyEvent event); this method is triggered when a user triggers a touch screen event on this component.
... And so on
MainActivity. java
public class MainActivity extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}}Main. xml
MyButton. java
Public class MyButton extends Button {public MyButton (Context context, AttributeSet set) {super (context, set) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {super. onKeyDown (keyCode, event); Log. v ("-crazyit.org-", "the onKeyDown in MyButton"); // returns true, indicating that the event will not spread outward and return true ;}}