Now to analyze and analyze the click method of the Android View onclick (); We know that the onclick () is a callback method, and when the user clicks on the view to execute this method, we use the button to give an example //this is a callback interface for view */ * * Interface definition for a callback to being invoked when a view is Clicked.*/public Interface Onclicklistener {/*** Calle D When a view have been clicked.** @param v The view that is clicked.*/void OnClick (view v);} package Com.example.demoactivity; import Android.app.activity;import Android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import android.widget.toast; /*** this is equivalent to Class A * @author xiaanming* implements the Onclicklistener interface----> Background a */public class Mainactivity extends Activity implements onclicklistener{/*** class A contains reference to class B-----> Background two */private Button button;& nbsp; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main), Button = (button) Findviewbyid (r.id.button1), /*** Class A calls the View method, and button extends View-----&Gt Class A invokes a method of Class B C*/button.setonclicklistener (this);} /*** The callback function that is called when the user clicks the button, you can do what you have to do * Here's what I do is use Toast hint onclick*/@Overridepublic void OnClick (View v) {Toast.maketext (Getapplication (), "OnClick", Toast.length_long). Show (); } below is the Setonclicklistener method of the view class, which is the equivalent of Class B, just paste the key code out/*** This view is equivalent to Class B * @author Xiaanming**/public class View implements Drawable.callback, Keyevent.callback, Accessibilityeventsource {/*** Listener used to dispatch click even ts.* This field should was made private, so it was hidden from the sdk.* {@hide}*/protected onclicklistener Monclicklistener ; /*** Setonclicklistener () parameter is Onclicklistener interface------> Background three * Register a callback to being invoked when this view is CL Icked. If This view was not* clickable, it becomes clickable.** @param l The callback that would run** @see #setClickable (Boolean) * / public void Setonclicklistener (Onclicklistener l) {if (!isclickable ()) {setclickable (true);} Monclicklistener = l;} /*** call this view ' s Onclicklistener, if it is a defined.** @return True there was a assigned onclicklistener that were called, false* otherwise is returned.*/p Ublic Boolean PerformClick () {sendaccessibilityevent (accessibilityevent.type_view_clicked); if ( Monclicklistener = null) {playsoundeffect (Soundeffectconstants.click); //This is not the equivalent of Class B calling a Class A method D, This d is called the callback method Monclicklistener.onclick (this); return true;} return false;}} This example is the typical Android callback mechanism, after reading this you do not further understand the callback mechanism? Thread run () is also a callback method, when the start () method that executes the thread will call back to the Run () method, and the processing message is more classic and so on
Callback in-depth understanding of synchronization callbacks View.onclicklistener as columns in Android