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