Android Development Habits Summary

Source: Internet
Author: User

Write a class for the interface, this class is different from activity or fragment, as long as the activity or fragment provide context parameters and root view parameters to it can realize the instantiation of various spaces, event settings monitoring

1. Constructor passed in parameter

public class Myview{private Context mcontext;private View Myringview;public MyView (context ctx, view view) {This.mcontext = Ctx;this.myview = View;initviews (); initevents (); Initdatas ();}}

2. Instantiating controls

public void Initviews () {Ivheadicon = (ImageView) Myview.findviewbyid (R.id.head_icon); ...}

3.3 common ways to set up monitoring

A. Declaring a private listener

public void Initevents () {Ivheadicon.setonclicklistener (Onclicklistener); ...}
Private Onclicklistener Onclicklistener = new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-genera Ted Method Stubif (v = = Ivheadicon) {} else if (v = = Tvusernick) {} ...}}

B. In this step of setting up the listener direct new an anonymous inner class, this method is more flexible, suitable for a small number of controls to add monitoring

C. Implement listener events for the entire view, implements Onclicklistener, and then re-onclick method

In the OnClick method, you can perform a specific response action by judging the ID of the control or by judging the object.

3. Initialize data

is to set the initial state of the control, the initial content



Android Development Habits Summary

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.