Android development often use a variety of listeners, Android Listener writing and Java have different places;
Using an inner class to implement an interface in 1,activity, creating an internal class instance using the Add method is similar to Java
Create an instance of the listener
Mylis lis = new Mylis ();
To add a listener to a button using the Add method
Internal class creation Listener
Android Code:
- Class Mylis implements onclicklistener{
- @Override
- public void OnClick (View v) {
- }
- }
2, anonymous class add listener
Android Code:
- Button btn = (button) Findviewbyid (R.ID.BTN);
- Btn.setonclicklistener (New Onclicklistener () {
- @Override
- public void OnClick (View v) {
- }
- });
3, Implement Interface
Adding the OnClick method to the 4,xml configuration file
Android Code:
- Configure the notation in the button XML
- android:onclick="Change"
- In the activity page, just create the method directly
- public void Change (View v) {
- }
Four Technologies for Android button listeners