Libgdx: 单击并展开 SelectBox 时,TextField 不会失去焦点。

创建于 2014-07-28  ·  3评论  ·  资料来源: libgdx/libgdx

操作系统: Windows 8.1、x64
gdx版本: 1.2.0
定位:仅限桌面(未在其他平台上测试)

当我单击 SelectBox 以显示下拉列表时; 相邻表格单元格中的 TextField 应该失去焦点,但事实并非如此。 这些组件共享一个表和一个舞台。 相互阶段处理输入。

这并没有真正破坏任何东西,但它是不受欢迎的行为,并且会使游戏看起来不专业,同样如此。

相关代码重现:

    import com.badlogic.gdx.scenes.scene2d.ui.*;

    final Label titleLabel = new Label(
            "Title: ", skin, "heading");

    final Label typeLabel = new Label(
            "Type: ", skin, "heading");

    final TextField textField = new TextField("", skin);

    final SelectBox<String> contextSelector = new SelectBox<String>(skin);
    contextSelector.setItems("Weapon", "Tool", "Crop", "Machine", "World Rule");

    table.row();
        table.add(titleLabel).right();
        table.add(textField).fill().prefWidth(Gdx.graphics.getWidth() / 5.0f).space(1.0f);
    table.row();
        table.add(typeLabel).right();
        table.add(contextSelector).fill().prefWidth(Gdx.graphics.getWidth() / 5.0f).space(1.0f);
    table.row().height(15.0f); table.add().fill();
    table.row();
        table.add(placeHolderLabel1);
        table.add(startButton).right();

最有用的评论

我不确定您是否总是希望 TextField 在单击另一个演员时失去焦点。 您可以轻松自定义行为:

stage.getRoot().addCaptureListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        if (!(event.getTarget() instanceof TextField)) stage.setKeyboardFocus(null);
        return false;
    }
}

所有3条评论

我不确定您是否总是希望 TextField 在单击另一个演员时失去焦点。 您可以轻松自定义行为:

stage.getRoot().addCaptureListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        if (!(event.getTarget() instanceof TextField)) stage.setKeyboardFocus(null);
        return false;
    }
}

嗯,这当然可以解决问题。 干杯。 :-)

我再次为我的问题找到了一个优雅的解决方案。 Scene2d 很棒:)

此页面是否有帮助?
0 / 5 - 0 等级