触摸移动TextField

时间:2015-02-27 20:18:38

标签: input libgdx scene2d

所以我希望能够在触摸时移动文本字段。此时此代码会在我触摸屏幕时立即移动。我希望它只在我接触IT时移动。 123123123123123123123123123

InputListener drag = new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            // TODO Auto-generated method stub
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
            // TODO Auto-generated method stub
            super.touchUp(event, x, y, pointer, button);
        }

        @Override
        public void touchDragged(InputEvent event, float x, float y,
                int pointer) {
            textField.setOrigin(Gdx.input.getX(), Gdx.input.getY());
            textField.setPosition(x, y);
            super.touchDragged(event, x, y, pointer);
        }





    };

    textField.addListener(drag);


    game.inputMultiplexer = new InputMultiplexer();


    game.inputMultiplexer.addProcessor(stage);
    game.inputMultiplexer.addProcessor(stagePurc);
    game.inputMultiplexer.addProcessor(stageText);





    Gdx.input.setInputProcessor(game.inputMultiplexer);

这里我将textField设置为它的阶段:

stageText.addActor(textField);
    stageText.addActor(textArea);

1 个答案:

答案 0 :(得分:1)

您不需要为InputProcessor创建新的TextField。它已经可以收听点击。

在您使用InputProcessor(或Gdx.input.setInputProcessor(stage),如果您愿意)将inputMultiplexer设置为舞台并将TextField添加到舞台后,您可以添加{{1}到你的InputListener

TextField

... TextField textField = new TextField("test", textFieldStyle); someTable.add(textField); InputListener mouseListener = new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { // Here you can do the movement of the textField // ... System.out.println("click"); return true; } }; textField.addListener(mouseListener); ... 方法中的代码仅在单击touchDown()时触发。