LibGDX - how to catch a backbutton inside a TextField with Android Keyboard visible?

自作多情 提交于 2019-12-24 07:29:43

问题


I'm trying to catch the Back button while the TextField is focused (keyboard is visible).

I have already tried with Multiplexer - setting the 'BackProcessor' on the top of the stages - it doesn't work:

InputProcessor backProcessor = new InputAdapter() {
        @Override
        public boolean keyDown(int keycode) {

            if ((keycode == Input.Keys.BACK) )
            {
                Gdx.app.log("INPUT", "BACK");
            }
            return false;
        }
    };

    InputMultiplexer multiplexer = new InputMultiplexer(backProcessor,
            loginStage,registerStage);
    Gdx.input.setInputProcessor(multiplexer);

Also, I tried in the render method with:

if(Gdx.input.isKeyDown(Keys.BACK)

Doesn't work too.

Above solutions work perfectly EXCEPT the moment, when the keyboard is visible.

What I'm trying to achieve? I need to catch the Back Button when the onScreenKeyboard is visible.

Edit

I also tried with TextFieldListener but 'BackButton' is the one key that hasn't any 'char code' so it can't be catched there:

public void keyTyped(TextField textField, char c)

FINAL EDIT

As LibGDX authors said - there's no way to retrieve this in a normal way cause the back button is proccessed outside of application while it's pressed when keyboard is visible. Android solution is to override EditText's onPreKeyIme() but LibGDX TextField has nothing to do with Android's one and there's no connection.

If there's anyone that could point any solution to this problem, I'd be grateful.


回答1:


Rewrite your GDX Launcher class using this tutorial: https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

Then, you'll have your RelativeLayout so you can override your dispatchKeyEventPreIme() method which will catch events before they're sent to the IME ;)

RelativeLayout layout = new RelativeLayout(this) {
            @Override
            public boolean dispatchKeyEventPreIme(KeyEvent event) {


                if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                    if(event.getAction() == KeyEvent.ACTION_DOWN){
                        // back pressed                 }

                }
                return super.dispatchKeyEventPreIme(event);
            }
        };



回答2:


The answer is - impossible. Android doesn't catch the back button while keyboard is active. Ha. They don't even have a way to check if soft-keyboard is visible and there are tricks to catch it's view's height (which gives the keyboard height) which also makes everything wrong because it collides with other ui components like navigation/status bar and the whole immersive mode.

I hope that someday some Google Dev will look here, check this and think about fixing this after 6 years, because we're in 2016 and you're fixing code-style instead of critical bugs which are posted on tracker by greatest coders from here.



来源:https://stackoverflow.com/questions/38880337/libgdx-how-to-catch-a-backbutton-inside-a-textfield-with-android-keyboard-visi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!