Java does not have a C # reference type, so event hooking is generally implemented using interfaces, in two ways:
1) Define a class that implements the event interface, then implement the interface method, and then add an instance of the class to the event listener:
public class ONCLICKLISTENERHDL implements View.onclicklistener { @Override public void OnClick ( View v) { //TODO auto-generated method stub } }
Use:
_button1.setonclicklistener (New ONCLICKLISTENERHDL ());
2) define an anonymous class to complete:
_button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub} });
In the 1th way, although it is relatively clear, but there is a very bad thing is that when the page event processing will be access to other page controls, this way will increase the delivery of many parameters. There is a very good design pattern for page processing-the mediator pattern (also called the Bazaar mode), where all page control interactions and event responses are placed in a single mediation class (typically the activity of the page).
The 2nd approach is better than the first, at least the aggregation of the code is high, and you can access certain public variables, but it is important to note that this in the event handling method does not refer to mediation activity.
The method binding mechanism in the Android view is the best
android:onclick= "DoClick"
As long as there is an DoClick method that conforms to the specification in the corresponding activity. This is similar to the way C # is handled, but its implementation is somewhat tortuous, and I have a simulation below:
@Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); _mainactivity = this; _abc= "Hello you is good!"; _button1 = (Button) This.findviewbyid (r.id.button1); Note the invocation here, which can be implemented dynamically when the XML file is loaded. _button1.setonclicklistener (Createclicklistener (This, "DoClick", _button1)); _button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub} });
This method is generic for the Click event, and other events can be handled similarly. Private Onclicklistener Createclicklistener (final Activity handler,final String methodname,final View v) {return New Onclicklistener () {@Overridepublic void OnClick (View v) {class<?> thetypeview=null;try {Thetypeview = Class.forName ("Android.view.View");} catch (ClassNotFoundException E1) {//TODO auto-generated catch block E1.printstacktrace ();} if (Thetypeview! = null) {try {Method theclickmethod= handler.getclass (). GetMethod (MethodName, Thetypeview); if (thec Lickmethod!=null) {Theclickmethod.invoke (Handler, v);}} catch (Nosuchmethodexception e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Illegalaccessexception e) {//TODO auto-generated The Catch block E.printstacktrace ();} catch (IllegalArgumentException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (InvocationTargetException e) {//TOD O Auto-generated catch block E.printstacktrace ();}} } }; public void DoClick (View Button2) {button TheButton2 = (button) Button2; ThebutTon2.settext ("Hello," +thebutton2.gettext ()); }
It's not clear that Android is dealing with a similar approach. But given the language mechanism of Java, it is estimated that this can only be done.
??
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Page Event hook-up simulation