【问题标题】:Moving TextField on touch触摸时移动 TextField
【发布时间】:2015-02-27 20:18:38
【问题描述】:

所以我希望能够在触摸文本字段时移动它。目前,只要我触摸屏幕,此代码就会使其移动。我希望它只在我触摸它时移动。 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);

【问题讨论】:

    标签: input libgdx scene2d


    【解决方案1】:

    您无需为您的TextField 创建新的InputProcessor。已经可以监听点击了。

    在您将舞台设置为InputProcessorGdx.input.setInputProcessor(stage)(或inputMultiplexer,如果您愿意)并将您的TextField 添加到舞台后,您可以将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()方法中的代码只有在textField被点击时才会触发。

    【讨论】:

    • 如果我将 InputProcessor 更改为 InputListener,我无法将它添加到 inputMultiplexer?因此,touchDown 不起作用。
    • 您不需要直接将TextField添加到inputMultiplexer。您必须将TextField 添加到stage 并将stage 添加到inputMultiplexor。就这样。 Stage 将自动向其内部的所有侦听器发送点击。
    • 我不确定你的意思。看看我的第一篇文章,考虑到你所说的,我已经将其更改为我现在所做的事情。使用该代码,触摸时 textField 不会移动。
    • 您是否将textField 添加到您的stage?这未反映在您的代码示例中。以及如何移动textField?我没有看到任何移动方法。
    • 1) 在您的问题中,您希望您的 textField 响应点击,但您将 setPosition() 添加到 touchDragged() 方法。为什么?你是拖动这个textField 还是点击它? 2) 方法setOrigin() 在这里看起来很奇怪,没有任何意义。仔细阅读有关它的文档。 3)设置断点或/和println()进行调试到touchDown()touchUp()touchDragged(),看看它们是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多