The first type: not easy to manage.
< Button Android:id = "@+id/btn_gridlayout" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " android:text=" Register with Grid layout " android:onclick=" GridLayout "/>
Public classMainactivityextendsActivity {@Overrideprotected voidOnCreate (Bundle savedinstancestate) {//after the view loads the first call method, the activity is created automatically, cannot new, can invoke the method to start him, Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main);//set the activity's layout to show the activity's layoutFindviewbyid (R.id.rbfemale);//in the ID of this class R this internal class to find this value, and then find this ID identifies something, the return value is the view } //The onclick value is a method of GridLayout the button's Click event Response Public voidGridLayout (View v) {//v is the button of the point, and view is the parent of all controlsLOG.I ("main", ((Button) v). GetText (). toString ()); Setcontentview (r.layout.gridlayout); }}
The second type: Anonymous inner class, if only used once, this is not convenient for code reuse.
The Third kind: the famous inner class, the code has the reusability.
The fourth type: Activity implements the Click Interface,
public void OnClick (View v) {//view is the base class for all layouts, switch (V.getid ()) {case R.id.btnlogin: toast.maketext (This, "Perform login Operation", "()." Show (); break; Case R.id.btnexit: finish (); break; } }
For a lot of buttons, and each Click event code is not much time to use, if the code is a lot, the swith changed very much is not suitable.
The fifth kind: The external class, has the reusability, facilitates the code confusion.
Android 13 5 Click events Comparison of different implementations