3

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

Prophet
  • 32,350
  • 22
  • 54
  • 79
shinigota
  • 53
  • 2
  • 7

1 Answers1

5

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.

kabb
  • 2,474
  • 2
  • 17
  • 24
  • Thanks for the answer ! But, actually I wanted to make toggle button so the texture for the pressed button could stay, but if I can't that's not a big problem – shinigota Jun 01 '14 at 21:23
  • It will stay unless you specify textures for the checked and unchecked state. – kabb Jun 01 '14 at 21:49