Android Page Event hook-up simulation

Source: Internet
Author: User

Java does not have a reference type for C #. As a result, event hooks are typically 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 self-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 self-generated method stub}                });

Although the 1th approach is relatively straightforward, there is a very negative place to visit other page controls when the page event is processed, in such a way that a very high number of parameters are passed. There is a very good design pattern for page processing--The mediator pattern (also called the Bazaar mode), the interaction of all page controls and the event response are placed in a mediation class (usually the corresponding activity on the page).
The 2nd approach is better than the first, at least the aggregation of the code is relatively high, and access to certain public variables, but it is important to note that in the event processing method This does not refer to the mediation activity.
The method binding mechanism in the Android view is the best
android:onclick= "DoClick"
There is only one DoClick method that conforms to the norm in the corresponding activity. This is almost identical to the way C # is handled, but in fact it is now expected to be more tortuous. I did a simulation of the following:

@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);        Notice the call here. This can be implemented dynamically when loading XML files.        _button1.setonclicklistener (Createclicklistener (This, "DoClick", _button1));        _button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {///TODO self-generated method stub}                }); c10/>}
    This method is generic for the Click event.    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 own actively 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 self-generated catch block E.printstacktrace ();} catch (Illegalaccessexception e) {//Todo Yourself The actively generated catch block E.printstacktrace ();} catch (IllegalArgumentException e) {//TODO self-generated catch block E.printstacktrace ();} catch (InvocationTargetException e) {//T        ODO own actively generated catch block E.printstacktrace ();}}    }       };   public void DoClick (View Button2) {button TheButton2 = (button) Button2;    Thebutton2.settext ("Hello," +thebutton2.gettext ()); }

As for Android, it is not handled in a similar way. It's not very clear.

But given the language mechanism of Java, it is expected to do just that.

??

Android Page Event hook-up simulation

Related Article

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.