Android page event hook simulation, android page
Java does not have a reference type for C #. Therefore, event mounting is generally implemented using interfaces. There are two methods:
1) define a class that implements the event interface, then implement the interface method, and add the instance of this class to the event listener:
Public class OnClickListenerHdl implements View. OnClickListener {@ Override public void onClick (View v) {// method stub automatically generated by TODO }}
Usage:
_ Button1.setOnClickListener (new OnClickListenerHdl ());
2) define an anonymous class to complete:
_ Button1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// method stub automatically generated by TODO }});
Although the 1st method is relatively clear, there is a very negative point, that is, when page events are processed, other page controls will be accessed. This method will add a lot of parameter passing. There is a good design pattern for page processing in this way-the intermediary pattern (also called the market pattern ), the interaction and Event Response of all page controls are placed in an intermediary class (generally the Activity corresponding to the page ).
The 2nd method is better than the first method. At least the Code has a high degree of polymerization and can access certain public variables, however, it should be noted that this in the event processing method does not refer to the intermediary Activity.
The method binding mechanism in the android view is the best
Android: onClick = "DoClick"
Only one conforming DoClick method is available in the corresponding Activity. This is similar to the processing method of C #, but its implementation estimation is twists and turns. I am doing a simulation below:
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); _ MainActivity = this; _ Abc = "hello you are good! "; _ Button1 = (Button) this. findViewById (R. id. button1); // note the call here, which can be dynamically implemented when an XML file is loaded. _ Button1.setOnClickListener (CreateClickListener (this, "DoClick", _ Button1); _ Button1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// method stub automatically generated by TODO }});}
// This method is common for Click events. Other events can be processed 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) {// catch Block e1.printStackTrace ();} if (theTypeView! = Null) {try {Method theClickMethod = Handler. getClass (). getMethod (MethodName, theTypeView); if (theClickMethod! = Null) {theClickMethod. invoke (Handler, v) ;}} catch (NoSuchMethodException e) {// catch Block e automatically generated by TODO. printStackTrace ();} catch (IllegalAccessException e) {// catch Block e automatically generated by TODO. printStackTrace ();} catch (IllegalArgumentException e) {// catch Block e automatically generated by TODO. printStackTrace ();} catch (InvocationTargetException e) {// catch Block e automatically generated by TODO. printStackTrace () ;}}};} public void DoClick (View Button2) {Button theButton2 = (Button) Button2; theButton2.setText ("hello," + theButton2.getText ());}
It is not clear whether Android uses a similar method for processing. But in view of the Java language mechanism, it is estimated that this can only be done.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.