【发布时间】:2014-03-05 08:54:05
【问题描述】:
我有一个舞台,上面有多个按钮,基本上用作工具箱。我希望用户能够在显示的不同项目之间进行选择;因此,当用户选择一项时,必须取消选择所有其他项。
我想用 libGDX 按钮的选中属性来做到这一点。但是,我不知道如何以编程方式取消选中按钮并以最简单的方式访问舞台上的所有演员。
我不能提供代码,因为正如我所说,我什至不知道如何取消选中按钮,而谷歌也无济于事。这甚至可能吗?如果没有,我会很高兴收到其他建议。
【问题讨论】:
我有一个舞台,上面有多个按钮,基本上用作工具箱。我希望用户能够在显示的不同项目之间进行选择;因此,当用户选择一项时,必须取消选择所有其他项。
我想用 libGDX 按钮的选中属性来做到这一点。但是,我不知道如何以编程方式取消选中按钮并以最简单的方式访问舞台上的所有演员。
我不能提供代码,因为正如我所说,我什至不知道如何取消选中按钮,而谷歌也无济于事。这甚至可能吗?如果没有,我会很高兴收到其他建议。
【问题讨论】:
看看一个ButtonGroup
ButtonGroup 不是演员,也没有视觉效果。按钮被添加到它,它强制检查按钮的最小和最大数量。这允许按钮(按钮、文本按钮、复选框等)用作“单选”按钮。 https://github.com/libgdx/libgdx/wiki/Scene2d.ui#wiki-ButtonGroup
还可以尝试查看有用的 javadocs http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ButtonGroup.html
基本上,您创建 ButtonGroup 添加参与者并设置应允许的最小检查量。
//initalize stage and all your buttons
ButtonGroup buttonGroup = new ButtonGroup(button1, button2, button3, etc...)
//next set the max and min amount to be checked
buttonGroup.setMaxCheckCount(1);
buttonGroup.setMinCheckCount(0);
//it may be useful to use this method:
buttonGroup.setUncheckLast(true); //If true, when the maximum number of buttons are checked and an additional button is checked, the last button to be checked is unchecked so that the maximum is not exceeded.
【讨论】: