【问题标题】:Getting the dimensions of the soft keyboard获取软键盘的尺寸
【发布时间】:2012-11-12 03:07:49
【问题描述】:

有没有办法知道屏幕上显示的键盘大小?

我正在使用Cocos2dx进行编程,但是我想知道Android部分或Cocos部分屏幕显示的键盘高度,没关系。

我知道 Keyboard 有一个 getHeight() 方法,但我不想创建新键盘,我想使用默认键盘。

【问题讨论】:

  • Do you want keyboard then cocos2d-x has been combined the android keyBoard you may check in the Test Classes -> TextInput where they are using android keyboard in cocos2d-x ...检查是否有帮助你
  • @Rudy_TM :有你需要的东西??? ...
  • 否:/我们无法获得键盘的高度,问题是如果文本字段在键盘后面,我们想向上推。我们在调用键盘时放置一个常量 'activity.getCurrentFocus().getMeasuredHeight()' 并将其乘以键盘键的行数,在智能手机中它可以工作,但在平板电脑中,它不起作用,所以那不是解决方案:/但同时我们将使用这个uu

标签: android keyboard cocos2d-x


【解决方案1】:

我们这样做了

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    Log.d("Keyboard Size", "Size: " + heightDifference);

                }
            });

我们只用键盘调整视图大小,所以我们可以使用它。

【讨论】:

  • 高差是px还是dp?
  • 它以像素为单位,因为高度测量返回像素数量。
  • 如果屏幕上显示的不仅仅是键盘,即系统按钮(返回、主页等),它将不起作用
  • 对我来说(带有 Android 5.0 的 Nexus 7)它确实考虑了系统按钮。
  • react-top 或 rect-bottom 中覆盖了哪些显示组件?
【解决方案2】:
Rect r = new Rect();
View rootview = this.getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);

结果是您的应用程序在屏幕上使用的空间量(即使在活动调整大小时也能正常工作)。显然剩余的屏幕空间将被键盘使用(如果它可见的话)

在这里找到 id:https://github.com/freshplanet/ANE-KeyboardSize/blob/master/android/src/com/freshplanet/ane/KeyboardSize/getKeyboardY.java

【讨论】:

  • 你确定键盘在那个时间点是可见的吗?我遇到了一些问题,我必须在更新活动布局时获取尺寸。
  • 感谢这个简单而有用的答案!这适用于使用屏幕系统按钮(如 LG)的手机吗?到目前为止,您对此解决方案没有任何问题?
  • 是的,我在开发游戏时使用了nexus 4,因此我不得不处理屏幕上的按钮。项目已暂停,但我仍将其安装在 Z5 上试一试 - 在 Android 6 上仍能正常运行
【解决方案3】:

如果您的活动不是全屏,请使用以下代码:

content.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    if (keyBoardHeight <= 100) {
                        Rect r = new Rect();
                        content.getWindowVisibleDisplayFrame(r);

                        int screenHeight = content.getRootView()
                                .getHeight();
                        int heightDifference = screenHeight
                                - (r.bottom - r.top);
                        int resourceId = getResources()
                                .getIdentifier("status_bar_height",
                                        "dimen", "android");
                        if (resourceId > 0) {
                            heightDifference -= getResources()
                                    .getDimensionPixelSize(resourceId);
                        }
                        if (heightDifference > 100) {
                            keyBoardHeight = heightDifference;
                        }

                        Log.d("Keyboard Size", "Size: " + heightDifference);
                    }
                    // boolean visible = heightDiff > screenHeight / 3;
                }
            });

【讨论】:

  • 谢谢,如果要翻译editview by Y,status_bar_height非常重要
【解决方案4】:

如果您想在活动大小不变的情况下计算虚拟键盘高度(adjustPan),那么您可以使用此示例:

https://github.com/siebeprojects/samples-keyboardheight

它使用隐藏窗口来计算窗口和活动根视图之间的高度差。

【讨论】:

    【解决方案5】:

    你说不出来。不,真的:你根本看不出来。

    键盘不需要是任何特定的形状。它不必放在屏幕底部(manymost popular 选项are not),当您更改文本字段时,它不必保持其当前大小(几乎没有,取决于标志)。它甚至不必是rectangular。它也可能只是接管entire screen

    【讨论】:

    • 您是否找到了解决方案,或者 android 无法提供真正的应用程序?我的意思是,在某种意义上是超级动态的,但不提供处理动态
    【解决方案6】:

    我知道这是一篇旧帖子,但我注意到为我选择的解决方案并非适用于所有设备。似乎存在差异,所以我实现了这个,它似乎是一个包罗万象的:

            final int[] discrepancy = new int[1];
            discrepancy[0] = 0;
    
            // this gets the height of the keyboard
            content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    View rootview = activity.getWindow().getDecorView(); // this = activity
                    rootview.getWindowVisibleDisplayFrame(r);
    
                    int screen_height = rootview.getRootView().getHeight();
                    int keyboard_height = screen_height - (r.bottom + r.top) - discrepancy[0];
    
                    if (discrepancy[0] == 0) {
                        discrepancy[0] = keyboard_height;
                        if (keyboard_height == 0) discrepancy[0] = 1;
                    }
    
                    int margin_bottom = keyboard_height + Helper.getDp(10, activity);
    
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) carousel_container.getLayoutParams();
                    params.setMargins(0, 0, 0, margin_bottom);
    
                    //boolean visible = heightDiff > screenHeight / 3;
                }
            });
    

    当第一次调用监听器时,它会在没有键盘的情况下测量屏幕,如果存在差异,我会在下一次考虑它。如果没有差异,我将差异设置为 1,这样它就不再是 0。

    【讨论】:

      【解决方案7】:

      在 cocos2d-x 中我们有 CCEditBox。

      在 Extensions->GUI->CCEditBox 里面,可以找到 CCEditBox 类。

      美妙之处在于它隐藏了在场景中其他地方敲击的键盘。并自动向上移动键盘,以防您的编辑框在场景中放置得太低。

      如果您使用的是 cocos2d-x v2.1.3,那么您可以通过转到

      导航到示例项目

      samples->cpp->TestCpp->Classes->ExtensionTest->EditBoxTest.

      从现在开始,我将只使用它而不是 CCTextField。昨天刚发现的:)

      【讨论】:

      • 很抱歉没有将其添加为评论,因为我根本做不到。
      【解决方案8】:

      2020年后,如果你的min SDK大于等于21,你可以通过以下函数查看IME的可见性和高度:

      fun isKeyboardVisible(attachedView: View): Boolean {
          val insets = ViewCompat.getRootWindowInsets(attachedView)
          return insets?.isVisible(WindowInsetsCompat.Type.ime()) ?: false
      }
      
      fun getKeyboardHeight(attachedView: View): Int {
          val insets = ViewCompat.getRootWindowInsets(attachedView)
          return insets?.getInsets(WindowInsetsCompat.Type.ime())?.bottom ?: 0
      }
      

      参考:Animating your keyboard (part 1). New WindowInsets APIs for checking the… | by Chris Banes | Android Developers | Medium

      【讨论】:

        【解决方案9】:

        经过数小时的搜索,我找到了一个解决方案如果你想设置windowSoftInput="adjustPan"

        这里是sn-p的代码:

            final View root  = findViewById(android.R.id.content);
            root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            Rect r = new Rect();
            {
                root.getWindowVisibleDisplayFrame(r);
            }
            @Override
            public void onGlobalLayout() {
                Rect r2 = new Rect();
                root.getWindowVisibleDisplayFrame(r2);
                int keyboardHeight = r.height() - r2.height();
                if (keyboardHeight > 100) {
                    root.scrollTo(0, keyboardHeight);
                }
                else {
                    root.scrollTo(0, 0);
                }
            }
        });
        

        在这段代码中,当我找到键盘高度后,我将视图向上滚动到未被键盘覆盖的位置,这是找到键盘高度的主要原因。

        根据docs

        void getWindowVisibleDisplayFrame(Rect outRect) : 检索此视图附加到的窗口所在的整体可见显示尺寸。

        【讨论】:

          【解决方案10】:

          Android 显示屏幕的 ROOT_VIEW 可以被可视化为带有 VISIBLE DISPLAY FRAME 的单个屏幕视图,它显示您的活动视图。

          当软键盘在屏幕上显示或隐藏时,会调整此可见显示框架。

          注意:请点击下面给出的链接查看两张图片,以便更好地理解

          因此,显示屏的 ROOT VIEW 可以可视化为: RootView of display screen

          VISIBLE DISPLAY FRAME 随着 SOFT KEYBOARD 的打开和关闭的调整可视化为: VISIBLE_DISPLAY_SCREEN adjustment

          VISUAL DISPLAY FRAME 的这种调整可以很好地用于找出键盘的高度:

          (当软键盘打开时)

          SOFT_KEYBOARD_HEIGHT = ROOT_VIEW_HEIGHT - (VISUAL_DISPLAY_FRAME_HEIGHT + EXTRA_SCREEN_HEIGHT)

          上面实现的代码是:

          int mExtraScreenHeight=-1, mKeyboardHeight=-1;
          boolean mKeyboardOpen;
          
          
          
              rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                  @Override
                  public void onGlobalLayout() {
          
                      int rootViewHeight, visibleDisplayFrameHeight, fakeHeight;
          
                      /* (rootViewHeight - visibleDisplayFrameHeight) is not the real height of the keyboard
                          it is the fake height as it also consist of extra screen height
                          so FAKE_HEIGHT = KEYBOARD_HEIGHT + EXTRA_SCREEN_HEIGHT
          
                          To get keyboard height extra screen height must be removed from fake height
                       */
          
                      Rect rect = new Rect();
                      rootView.getWindowVisibleDisplayFrame(rect);
          
                      rootViewHeight = rootView.getRootView().getHeight();
                      visibleDisplayFrameHeight = rect.height();
          
                      fakeHeight = rootViewHeight-visibleDisplayFrameHeight;
          
                      if (mExtraScreenHeight == -1){
                          mExtraScreenHeight=fakeHeight;
                      }
                      /* Suppose the soft keyboard is open then the VISIBLE_DISPLAY_FRAME is in reduced size
                          due to the space taken up by extra screen and the keyboard but when the soft keyboard closes
                          then KEYBOARD_HEIGHT=0 and thus FAKE_HEIGHT = EXTRA_SCREEN_HEIGHT
                       */
                      else if (fakeHeight <= mExtraScreenHeight){
                          mExtraScreenHeight=fakeHeight;
                          mKeypadOpen=false;
                      }
                      else if (fakeHeight > mExtraScreenHeight){
                          mKeypadHeight=fakeHeight-mExtraScreenHeight;
                          mKeypadOpen=true;
                      }
                  }
              });
          

          注意 : onGlobalLayout() 函数只会在全局布局发生变化时调用,比如软键盘打开时。所以软键盘必须至少打开一次才能获得软键盘高度。

          它对我有用;)

          【讨论】:

            【解决方案11】:

            抱歉无法发表评论,其中两三个答案帮助我解决了我的问题,它们与使用 AddOnGlobalLayoutListener 然后确定键盘出现前后的剩余高度有关。

            我使用的解决方案基于 Rudy_TM 的回答。

            但是,我必须找到的一件事是,为了使该方法起作用,您必须在某处具有以下行

            Window.SetSoftInputMode(SoftInput.AdjustResize);
            

            在我有 SoftInput.AdjustNothing(或类似的东西)之前,它不会工作。现在它完美无缺。感谢您的回答!

            【讨论】:

              【解决方案12】:

              完整的答案并为我完美工作:

                Rect r = new Rect();
                View rootview = this.getWindow().getDecorView(); // this = activity
                rootview.getWindowVisibleDisplayFrame(r);
                int keyboardHeight = rootview.getHeight() - r.bottom;
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-02-14
                • 2019-06-06
                • 2015-08-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-03-18
                • 2011-02-15
                相关资源
                最近更新 更多