单击时更改 TextButton 的字体颜色?

Change font color of TextButton on click?

我已经阅读了一些关于 scene2d 的 UI 功能的教程和文档,但我不太明白皮肤是如何工作的。我只想要一个在按下时改变颜色的 TextButton。我设法让 TextButton 的背景发生变化,但这不是我想要的。它应该没有背景。

如果有人能举例说明如何做到这一点,我将不胜感激。这看起来很简单,所以我想我在这里遗漏了一些明显的东西。如果涉及皮肤,请编程编写。

谢谢。

原来是我想的那么简单,就是第一次试的时候不行。

定义 TextButtonStyle 时,您可以指定 downFontColor 您希望文本在按下时显示的颜色。您还可以指定 checkedFontColor 想要文本在按下时切换的颜色。

示例:

TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.font = someBitmapFont;
textButtonStyle.fontColor = Color.WHITE;
textButtonStyle.downFontColor = Color.BLACK;
//Optional color to toggle between when pressed
textButtonStyle.checkedFontColor = Color.GREEN;
final TextButton textButton = new TextButton("Text", textButtonStyle);

这将产生一个白色的 TextButton,当它被按下时会变成黑色。当 mouse/touch 被释放时,它会变成绿色或白色,具体取决于切换的状态。

你也可以给它上色,但是效果取决于基色(如果是白色,那么效果很好,否则就看情况了)。反正就是这样搞的:

button.getLabel().setColor(myColor);