Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making "toggle buttons" with LibGDX

I'm trying to make sort of toggle buttons with LibGDX, so I searched how I could do them, and I found the ToggleButton class, but I suppose it's old since I don't have it in the last build...
So I'm trying to do them this way:

final TextButton button = new TextButton(weapon.getName(), skin2, "buy");

            button.addListener(new ClickListener() {
                    @Override
                    public void clicked(InputEvent event, float x, float y) {
                        if(button.isChecked()){
                            button.setChecked(false);
                            System.out.println("unchecked");
                        } else {    
                            button.setChecked(true);
                            System.out.println("checked");

                        }
                    }
              });

Actually, it keeps telling me unchecked, as if my button was always unchecked, so the setChecked method doesn't seems to word ...
I tried the toggle method, and it doesn't help at all, and I didn't found any other solution ...
So i was wondering if you had any idea of how I should do this !

Thanks for your help ! :)

like image 658
shinigota Avatar asked Dec 21 '25 04:12

shinigota


1 Answers

The button is toggled automatically when clicked, you don't have to add another listener to do it manually.

So the reason it only prints "unchecked" is because the Button checks itself when clicked, and then your listener is called, which just unchecks it immediately.

like image 191
kabb Avatar answered Dec 23 '25 19:12

kabb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!