Android 13 5 Click events Comparison of different implementations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.