When we write code, the most common thing we encounter is the listener. In practice, we also need to monitor the event. And some events are the business logic that needs to be realized, following things change dynamically. If we want to achieve an event, there is a position of monitoring, the monitoring of the color, the monitoring of the coordinates, there is the speed of monitoring, so many listening events. Then we need so many listeners. How are these listeners managed? We can create a mysterious role like the administrator, a listener pool that says a listener pool that can remove and add listeners. When we trigger an event, we need all of these listeners to perform a listener.
Now let's simulate the implementation of button buttons.
Create a new interface for the Click event Clickable.java. Only the Click event is performed.
Public interface Clickable { //click event void OnClick ();}
Create a New button class MyButton and implement the Click event interface clickable, which requires us to implement the OnClick method.
public class MyButton implements clickable { @Override public void OnClick () { //execute all listener methods, listener 1, listener 2 ... c3/>}}
So, what's the listener doing now? We all know that listeners are interface objects. is equivalent to an administrator. Clicking an event's interface object is equivalent to an event. The specific content of the event is represented by the method onclick ().
So since the administrator manages the event, we first create the Administrator (Listener interface) and then manage the Thing (OnClick ()).
The listener interfaces are as follows:
Public interface Onclicklistener { void OnClick (clickable clickable);}
At the same time, we need to increase the listener and write a method for setting up the listener. Callbacks are made to this interface.
</pre><pre name= "code" class= "java" > Public void Setonclicklistener (Onclicklistener onclicklistener { if (onclicklistener!=null) { Onclicklistener.onclick (this); } }
In this case, when we set the listener, the Onclicklistener interface object imported from the outside is not empty, and the OnClick () method in the Onclicklistener interface is executed.
Next, we can implement this listener interface as a way to get the Onclicklistener interface object. This place may be more difficult to understand. We should know that if a class implements an interface, it gets the corresponding interface object by instantiating the class object.
Let's create a new color listener. If the color changes, here I default to True. In fact, the color can be judged here. When there is a change, we need to expose a method to the outside world, so we create a new abstract method inside the class. This class is also implemented when the object is externally constructed anonymously.
public static abstract class Oncolorlistener implements onclicklistener{ @Override public void OnClick ( Clickable clickable) { if (true) { colorchanged (); } } Public abstract void colorchanged (); }
Next we'll create a new location listener. Same as above, the code is as follows:
public static abstract class Oncoordinatelistener implements onclicklistener{ @Override public Void OnClick (clickable clickable) { if (true) { coordinatechanged (); } } Public abstract void coordinatechanged (); }
And then we need a focus listener. Ditto
public static abstract class Onfocuslistener implements onclicklistener{ @Override public void OnClick ( Clickable clickable) { if (true) { onfocuschanged (); } } Public abstract void onfocuschanged (); }
These three classes implement the Onclicklistener interface, so we can get the Onclicklistener interface object simply by constructing the three objects anonymously. It also realizes the interface method inside the Onclicklistener. In this method, we separately monitor the change of the corresponding monitoring purposes. When XXX changes, we provide a way for the caller to implement it. So the class is changed to an abstract class, and an abstract method is provided. How the outside world calls it. We can see the Setonclicklistener method
public void Setonclicklistener (Onclicklistener onclicklistener) { if (onclicklistener!=null) { Onclicklistener.onclick (this); } }
As we see here, we know that the incoming Onclicklistener interface object can be obtained either by its specific implementation class's anonymous object or by its own interface object. So the parameters passed in can be the following
- Onclicklistener
- Focuslistener
- Oncoordinatelistener
- Oncolorlistener
These kinds of anonymous objects. Its essence is the Onclicklistener object. Why is it possible to pass three of the back?
Onclicklistener Onclicklistener = new Focuslistener ();
You can also
Onclicklistener Onclicklistener () = new Oncoordinatelistener ();
Or the other two kinds of anonymous objects are also possible.
Well, so far, our complete code is like this.
public class Button implements clickable{String color; int x, y; public void Setonclicklistener (Onclicklistener onclicklistener) {if (onclicklistener!=null) {Onclickliste Ner.onclick (this); }} @Override public void Click () {//Perform all listener actions} public interface onclicklistener{public VO ID OnClick (clickable clickable); } public static abstract class Oncolorlistener implements onclicklistener{@Override public void OnClick (clickable clickable) {if (true) {colorchanged (); }} public abstract void colorchanged (); } public static abstract class Oncoordinatelistener implements onclicklistener{@Override public void on Click (clickable clickable) {if (true) {coordinatechanged (); }} public abstract void coordinatechanged (); } public static abstract class Onfocuslistener implements OnClicklistener{@Override public void OnClick (clickable clickable) {if (true) {Onfocusch Anged (); }} public abstract void onfocuschanged (); }}
I think everyone can understand the above code. Next, we need to manage the interface objects of these listeners, we can manage them separately and manage them separately, how to manage these interface objects? We can create a new list collection object for three listeners. Increase its add and remove methods at the same time. But we don't have to do this, why? As I explained above, the Oncolorlistener object or Oncoordinatelistener, which is constructed anonymously, is also a Onfocuslistener interface object, which is essentially a Onclicklistener interface object. So we just need a collection to manage these listeners.
private static list<onclicklistener> onclicklisteners =new arraylist<onclicklistener> (); private void Addclicklistener (Onclicklistener onclicklistener) { onclicklisteners.add (onclicklistener); } private void Remove (Onclicklistener onclicklistener) { onclicklisteners.remove (onclicklistener); }
So we can be free to manage these listeners, and we know that when we setonclicklistener a new Onclicklistener anonymous object, we need to add it to the pool, which is the list collection. The code is as follows:
public void Setonclicklistener (Onclicklistener onclicklistener) { if (onclicklistener!=null) { Onclicklistener.onclick (this); } Addclicklistener (Onclicklistener); }
Ok. Having said so much, take a look at our complete code at this time.
Package Com.example.chenlei.myapplication.widget;import java.util.arraylist;import java.util.list;/** * Created by Chenlei on 2016/10/24. */public class Button implements clickable{String color; int x, y; public void Setonclicklistener (Onclicklistener onclicklistener) {if (onclicklistener!=null) {Onclickliste Ner.onclick (this); } addclicklistener (Onclicklistener); } @Override public void Click () {//Perform all listener actions} public interface onclicklistener{public void O Nclick (button button); } private static list<onclicklistener> onclicklisteners =new arraylist<onclicklistener> (); private void Addclicklistener (Onclicklistener onclicklistener) {onclicklisteners.add (Onclicklistener); } private void Remove (Onclicklistener onclicklistener) {onclicklisteners.remove (Onclicklistener); } public static abstract class Oncolorlistener implements onclicklistener{@Override Public void OnClick (Button button) {if (true) {colorchanged (); }} public abstract void colorchanged (); } public static abstract class Oncoordinatelistener implements onclicklistener{@Override public void on Click (Button button) {if (true) {coordinatechanged (); }} public abstract void coordinatechanged (); } public static abstract class Onfocuslistener implements onclicklistener{@Override public void OnClick (Button button) {if (true) {onfocuschanged (); }} public abstract void onfocuschanged (); }}
What should we do if we need to do all the listening work?
@Override Public Void Click () { //Perform all listener actions for (Onclicklistener onclicklistener:onclicklisteners) { Onclicklistener.onclick (this); } }
All we have to do is walk through the listener in the collection. Well, then let's see how to use it!
public class Mainactivity extends Appcompatactivity {private TextView TX; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); tx = (TextView) Findviewbyid (R.ID.TX); Button button =new button (); Button.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (Button button 1) {LOG.E ("TAG", "I am the callback method"); } }); Button button1 =new button (); Button1.setonclicklistener (New Button.oncolorlistener () {@Override public void colorchanged () { LOG.E ("TAG", "The color changes need to dry god horse"); } }); Button button2 =new button (); Button2.setonclicklistener (New Button.oncoordinatelistener () {@Override public void Coordinatechang Ed () {LOG.E ("TAG", "coordinate changes need to dry god horse"); } }); Button butTon3 =new Button (); Button3.setonclicklistener (New Button.onfocuslistener () {@Override public void onfocuschanged () { LOG.E ("TAG", "after the focus changes need to dry god horse"); } }); }}
The printed log results are as follows:
Uncover the veil of observer design patterns and teach you to write listeners