如何在Libgdx的文本框中设置光标?如何解决看不到光标的问题?

4
我尝试创建了一个可绘制的九宫格,并在textfieldstyle中完成了这个操作。
greenTextFieldStyle.cursor = getSkin().getDrawable("textFieldCursor1");

但是我看不到任何光标...

这是当前的光标图像,我也尝试过一个1 * 20的图像但没有成功。

它真的很小

基本上是一个3 * 3的图像,在中间有一个+号,在九宫格外有点。

1个回答

4

使用 getSkin().newDrawable("textFieldCursor1",Color.green),并为其添加最小宽度 greenTextFieldStyle.cursor.setMinWidth(2f);

这里提供一个包含"selectionframe"的完整皮肤:

Skin skin = new Skin();
skin.add(
        "background",
        new NinePatch(this.game.manager.get("hud/ninepatchframe.png",
                Texture.class), 5, 5, 5, 5));
skin.add("cursor", this.game.manager.get("data/cursor.png"));

TextFieldStyle tStyle = new TextFieldStyle();
tStyle.font = getDefaultFont(25); //here i get the font
tStyle.fontColor = Color.BLACK;
tStyle.background = skin.getDrawable("background");
tStyle.cursor = skin.newDrawable("cursor", Color.GREEN);
tStyle.cursor.setMinWidth(2f);
tStyle.selection = skin.newDrawable("background", 0.5f, 0.5f, 0.5f,
        0.5f);

this.nameField = new TextField("enter name here", tStyle);

这是我获取位图字体的方法。我使用来自libgdx的扩展.ttf创建它。 请查看链接了解如何添加原生文件。

这是使用它的代码:

public BitmapFont getDefaultFont(int size) {
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
            Gdx.files.internal("fonts/font.ttf"));
    defaultFont = generator.generateFont(size + 5); //some offset
    return defaultFont;
}

谢谢,setMinWidth(2f)对我有用!另外,我可以看到你在使用某种方法获取带有大小的默认字体,那么如何为位图字体设置大小呢? - thetheodor
已将其添加到答案中。我正在使用gdx-freetype扩展。代码可能已过时。如果我没记错的话,我的游戏是在9.7.x版本上运行的。 - bemeyer
谢谢,我会去看一下!你能帮忙回答这个问题吗?http://stackoverflow.com/questions/21318030/cant-draw-a-ninepatch-image-and-stage-at-the-same-time - thetheodor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接