1. Initialize the control you currently need, how to initialize a control ... private Button xxxx
Findviewbyid---Return the object of a view ...... Need to strongly turn into its subclass button object
Findviewbyid---is how to find the ID of the view ... Through the ID in R
2. Set the listener of the button to implement what we need to do to click on the button via the listener
Way One:
Anonymous inner classes implement event snooping:
New A Onclicklistener class in the Setonclicklistener method of the button, and write the code that needs to be implemented in the implementation of the Onclicklistener class.
Such as:
Button Loninbutton = (button) findviewid (R.ID.BT1);
Loginbutton.setonclicklistener (New Onclicklistener () {implemented content});
Way two:
External Class Listener events:
There are multiple buttons, each with an identical action, and each button has its own action, which uses an external class to listen for events.
Add a Myonclicklistener class that implements the Onclicklistener interface
Rewrite the onclick () method to add actions that need to be implemented in the method body;
Then implement the individual actions under each interface, such as:
Bt2.setonclicklistener (New Myonclicklistener () {
public void OnClick (View v) {
Super.onclick (v);//Implement the OnClick () of the parent class; action
Toast.maketext (Mainactivity.this, "BT3 logic to execute", Toast.length_short). Show ();
} });
Way three:
Interface mode for event monitoring:
The Activitymain class implements the Onclicklistener interface and then overrides the OnClick () of the Onclicklistener interface;
At the time of invocation, write this directly, such as:
Imgbt.setonclicklistener (this);
Mode four:
Independent events
Loginbutton.setonclicklistener (listener);
Externally separate:
Onclicklistener listener = new Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
System.out.println ("Class of the standalone implementation triggered");
}
};
Four ways to monitor event implementations