four ways to read Android Click events:
1. Implement the Onclicklistener interface in the form of an internal class, define the Click event
Class mainactivity extents activity{//... private class MyListener implements Onclicklistener{public void Onclick (View v) {// ... Click the response of the event}}}
2. Using Anonymous internal classes
Cons: Too many anonymous internal classes are required if there are too many click events
Class mainactivity extents activity{protected void onCreate (...) {//The Bt_call.setonclicklistener (new onclicklistener{public void Onclick (View v) {//...) is implemented as an anonymous inner class. Click the response of the event}});}}
3. Implement the Mainactivity Onclicklistener interface, a very common notation in practical development
Pros: You can solve the problem with method 2
Class mainactivity extents Activity implements onclicklistener{protected void OnCreate (...) {// ...} /*** Click event */public void OnClick (View v) {//based on different Id to determine what action to complete switch (V.getid ()) {case r.id.bt_call:callphone ();//Custom function, Make a call to break;
4. The method of binding a click event in the configuration file, not commonly used
Activity_main.xml:
<button android:onclick= "Callphone"//The Click event that binds the button will automatically find the method in Activity .../>
Mainactivity.java:
Class Mainactivity extents Activity {/*** Click event, in which case the method must have parameter View v*/public void Callphone (View v) {//...}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Four ways to read Android Click events