shinigota
shinigota

Reputation: 53

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 ! :)

Upvotes: 3

Views: 3257

Answers (1)

kabb
kabb

Reputation: 2502

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.

Upvotes: 5

Related Questions