忽略接口中实现的方法?

时间:2014-07-21 10:15:10

标签: java android libgdx

我有一个输入处理器界面,可以在屏幕上监听并向按钮类提供反馈,我无法控制界面中创建的方法。现在我正在尝试编写一个按钮类,以便我可以在类中声明它并仅调用touchUp和touchDown,这样我就不会受到代码污染。

以下是我试图减少的内容:

Button button = new Button();
        button.setInputListener(new InputProcessor() {
            @Override
            public boolean keyDown(int keycode) {
                return false;
            }

            @Override
            public boolean keyUp(int keycode) {
                return false;
            }

            @Override
            public boolean keyTyped(char character) {
                return false;
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                return false;
            }

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                return false;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                return false;
            }

            @Override
            public boolean mouseMoved(int screenX, int screenY) {
                return false;
            }

            @Override
            public boolean scrolled(int amount) {
                return false;
            }
        }
        );

在按钮类中,我有以下代码:

public void setInputListener(InputProcessor inputProcessor){
    this.inputProcessor = inputProcessor;
}

1 个答案:

答案 0 :(得分:0)

我认为任何其他人都坚持这一点。你必须创建另一个实现该界面的类,并确保它是抽象的,这是一个例子:

public abstract class ButtonListener implements InputProcessor {
    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

然后你可以在任何地方做到这一点,

Button button = new Button();
        button.setInputListener(new ButtonListener() {
                @Override
                public boolean keyDown(int keycode) {
                    return super.keyDown(keycode);
                }
            }
        );