【问题标题】:Android Libgdx: How to detect when a soft keyboard will be closed by hitting the BACK-ButtonAndroid Libgdx:如何通过点击 BACK 按钮来检测软键盘何时关闭
【发布时间】:2015-08-07 03:55:04
【问题描述】:

在我的 libgdx 项目(适用于 android)中,我使用的是 TextField。当 TextField 获得焦点时,会出现软键盘。一切都还好。现在,当用户点击智能手机的 BACK 按钮时,键盘就会消失。我怎样才能赶上那个事件?有人对这种情况有任何想法或经验吗?

P.S.:我设置了 Gdx.input.setCatchBackKey(true) ,它实际上只会阻止我的应用程序退出。因此,当用户在键盘可见的情况下点击 BACK 按钮时,它不会触发。

最好, 星际饼干

【问题讨论】:

    标签: android events keyboard libgdx back-button


    【解决方案1】:

    这是一个似乎对我有用的解决方法:

    首先,创建以下方法并从应用程序的onCreate() 方法中调用它:

    private static final int KEYBOARD_HEIGHT_THRESHOLD = 150;
    private static final int CHECK_KEYBOARD_DELAY = 200; //ms
    private void addKeyboardListener() {
            final View rootView = graphics.getView();
            final Rect layoutChangeRect = new Rect();
    
            rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(CHECK_KEYBOARD_DELAY);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            rootView.getWindowVisibleDisplayFrame(layoutChangeRect);
                            if (Math.abs(rootView.getHeight() - layoutChangeRect.height()) < KEYBOARD_HEIGHT_THRESHOLD) {
                                //keyboard closed
                            } else {
                                //keyboard opened
                            }
                        }
                    }).start();
                }
            });
    

    我已经为CHECK_KEYBOARD_DELAYKEYBOARD_HEIGHT_THRESHOLD 使用了对我来说可以的任意值。这些值似乎是必要的,因为键盘是动画的。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以创建一个实现InputProcessor 接口的类并捕获返回键被触摸的事件。像这样的东西(基于 wiki 中的示例):

      public class MyInputProcessor implements InputProcessor {
        // ... Rest of the InputProcessor methods
        @Override
         public boolean keyDown(int keycode) {
              if(keycode == Keys.BACK){
                 // This will be executed when back button is pressed
                 return true; // Means this event has been handled
              }
              return false;
         }
         // ...
      }
      

      如果你只需要捕获返回键事件,那么更简洁的方法是扩展InputAdapter,它基本上允许你只覆盖你需要的方法,而不是实现整个InputProcessor接口:

      public class MyInputProcessor extends InputAdapter {
        @Override
         public boolean keyDown(int keycode) {
              if(keycode == Keys.BACK){
                 // This will be executed when back button is pressed
                 return true; // Means this event has been handled
              }
              return false;
         }
      

      别忘了设置输入处理器:

      MyInputProcessor inputProcessor = new MyInputProcessor();
      Gdx.input.setInputProcessor(inputProcessor);
      

      【讨论】:

      • 感谢您的详细解释,user2016436。我听从了你的建议。不幸的是,当软键盘可见时,事件侦听器仍然没有检测到后退按钮的点击。当我点击后退按钮时,键盘关闭,但在我的实现中没有触发任何事件。为了确保实现的侦听器真正检测到 keyDowns,我添加了一个打印输出来查看事件是否检测到。
      • ... 当我按下测试设备的软键盘上的任何键时,该事件会正确触发,但当我按下测试设备的后退按钮时(我使用的是 Sony Xperia z2)。我想知道,在我的情况下是否有可能检测和处理该事件(?)。
      • @Starcracker 也许尝试以下解决方案之一:stackoverflow.com/questions/4312319/…stackoverflow.com/questions/3793093/…
      • 我基本上理解您建议的帖子背后的想法(非常感谢您!),但我想知道,在 libgdx 项目中实现它的有效方法是什么?我应该检查(例如)渲染功能中更改的视图大小吗?
      • 这对我也不起作用,当键盘显示并且后退按钮被触发时 - 没有输入。
      猜你喜欢
      • 2020-07-29
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多