【问题标题】:Libgdx How to implement list of checked buttons?Libgdx 如何实现选中按钮列表?
【发布时间】:2014-08-27 03:37:03
【问题描述】:
如何实现一个水平对齐的按钮列表,其中一次只能检查一个按钮?我想为一个游戏实现这个,你必须从列表中选择工具并单击一个对象来执行一个动作。我想突出显示代表当前工具的按钮,当您选择另一个按钮时,会将前一个工具返回到非突出显示状态,而将已选择的按钮返回到突出显示状态。因此,在任何给定时刻只能有一个工具处于活动状态,并且在您使用该工具并且没有选择其他东西之后,应该没有突出显示的按钮。这两天我尝试自己实现它,但我做不到。我认为在 libgdx scene2d 中可能有类似的东西,但我没有找到任何东西。任何帮助或建议将不胜感激。
【问题讨论】:
标签:
java
user-interface
libgdx
scene2d
【解决方案1】:
听起来像是ButtonGroup 的工作。我很惊讶在基本的谷歌搜索后没有出现。
在此处查看答案:LibGDX: Make all actors on a stage unchecked
//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.
【解决方案2】:
就像 Kevin Workman 所说的那样,使用 ButtonGroup 使选择起作用。要水平布局按钮,请使用基本表或 HorizontalGroup。要使突出显示工作,您可以使用 libgdx UI 工具包的 ImageButton 类,并使用您的普通图像来检查按钮的状态。对于未选中状态,您只需将该图像着色为浅灰色。请参阅 libgdx 中皮肤的用法以获取更多参考。
https://github.com/libgdx/libgdx/wiki/Skin