Java libgdx problem management single clicklistener

In libgdx, I try to set clicklistener to a specific situation when I press a specific button

public void setExamine(final ClickListener cl) {
        examine.addListener(cl);
}

This is the code I get from one class, which can be accessed from another class by doing the following:

table.setExamine(new ClickListener(){...});

However, doing so means that it will add a new clicklistener at a time

In addition to this, what other way can it manage a click listener instead of adding a click listener? I tried it

public void setExamine(final ClickListener cl) {
            examine.clearListeners();
            examine.addListener(cl);
}

But it seems to completely remove all the functions of the button click

resolvent:

You can save the "CL" reference passed to the setexamine method in the class. Then, you can first delete this specific listener in the setexamine method. I assume the check is a scene2d actor

private ClickListener clickListener;

public void setExamine(final ClickListener cl) {
   if(clickListener != null) {
      examine.removeListener(clickListener);
   }
   examine.addListener(cl);
   clickListener = cl;
}

If you want to apply a solution such as clearlisteners (), you should keep the default listener for this button

while(examine.getListeners().size > 1) {
   examine.removeListener(examine.getListeners()[1]);
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>