Benni
Benni

Reputation: 969

Moving TextField on touch

So I want to be able to move a textfield when I touch it. At the moment this code makes it move as soon as I touch the screen. I want it only to move as I touch 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);

Here I set the textField to it´s stage:

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

Upvotes: 0

Views: 126

Answers (1)

Rara
Rara

Reputation: 649

You don't need to create new InputProcessor for your TextField. It is already can listen for clicks.

After you set stage as an InputProcessor with Gdx.input.setInputProcessor(stage) (or with inputMultiplexer if you wish) and added your TextField to the stage you can add InputListener to your 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);

    ...

Code in this touchDown() method will trigger only when textField is clicked.

Upvotes: 1

Related Questions