(a) The original Java writing (believe that many have learned Java):
Need to implement interface View.ionclicklistener, preferably also inherit class: Activity, because View.ionclicklistener interface and inherit Ijavaobject, IDisposable interface, So also learn to implement the two interfaces inside the members, and the activity has been implemented by the two interface members, we do not need to write again, after all, most of us just want to rewrite the view.ionclicklistener inside the onclick function. (If the custom class simply implements the View.ionclicklistener interface, does not inherit the activity, it needs to implement the other 2 interface members, the interface is selected in VS, using the shortcut key ALT+SHIFT+F10, can quickly implement the interface)
The code is as follows:
1 usingSystem;2 usingAndroid.app;3 usingandroid.content;4 usingAndroid.runtime;5 usingandroid.views;6 usingAndroid.widget;7 usingAndroid.os;8 9 namespaceApp2Ten { One[Activity (Label ="App2", Mainlauncher =true, Icon ="@drawable/icon")] A - - Public classmainactivity:activity, View.ionclicklistener the { - button button; - Public voidOnClick (View v) - { +button. Text =string. Format ("{0} clicks!", v.id); - } + protected Override voidOnCreate (Bundle bundle) A { at Base. OnCreate (bundle); - - //Set Our view from the "main" layout resource - Setcontentview (Resource.Layout.Main); - - //Get Our buttons from the layout resource, in //And attach an event to it -button = findviewbyid<button>(Resource.Id.MyButton); tobutton. Setonclicklistener ( This); + - } the } *}
Of course you can define a class yourself, implement an interface, override the OnClick function, and then button. Setonclicklistener (an instance of your custom class);
(ii) Next introduction to C # 4 ways of writing (in fact, very similar)
1. The first (there is the creation of the template):
1 Button button = findviewbyid<button>(Resource.Id.MyButton); 2 Delegate string. Format ("{0} clicks! ", count++);};
2. The second type of
1 namespaceApp22 {3[Activity (Label ="App2", Mainlauncher =true, Icon ="@drawable/icon")]4 5 6 Public classmainactivity:activity, View.ionclicklistener7 {8 intCount =1;9 button button;Ten One protected Override voidOnCreate (Bundle bundle) A { - Base. OnCreate (bundle); - the //Set Our view from the "main" layout resource - Setcontentview (Resource.Layout.Main); - - //Get Our buttons from the layout resource, + //And attach an event to it -button = findviewbyid<button>(Resource.Id.MyButton); +button. Click + =Button_Click; A at } - - Private voidButton_Click (Objectsender, EventArgs e) - { -button. Text =string. Format ("{0} clicks!", count++); - } in - to } +}
3. The third type of
1 Public classmainactivity:activity, View.ionclicklistener2 {3 intCount =1;4 button button;5 6 protected Override voidOnCreate (Bundle bundle)7 {8 Base. OnCreate (bundle);9 Ten //Set Our view from the "main" layout resource One Setcontentview (Resource.Layout.Main); A - //Get Our buttons from the layout resource, - //And attach an event to it thebutton = findviewbyid<button>(Resource.Id.MyButton); -button. Click + =NewEventHandler (button_click); - - } + - Private voidButton_Click (Objectsender, EventArgs e) + { Abutton. Text =string. Format ("{0} clicks!", count++); at } - - -}
View Code
4. The fourth type
1 namespaceApp22 {3[Activity (Label ="App2", Mainlauncher =true, Icon ="@drawable/icon")]4 5 6 Public classmainactivity:activity, View.ionclicklistener7 {8 intCount =1;9 button button;Ten One protected Override voidOnCreate (Bundle bundle) A { - Base. OnCreate (bundle); - the //Set Our view from the "main" layout resource - Setcontentview (Resource.Layout.Main); - - //Get Our buttons from the layout resource, + //And attach an event to it -button = findviewbyid<button>(Resource.Id.MyButton); +button. Click + = (sender, e) =>{button. Text =string. Format ("{0} clicks!", count++);}; A at } - } -}
View Code
Mono for Android,xamarin Multiple-click event notation