获取片段软键盘高度 [英] Get Soft Keyboard Height in fragment

查看:225
本文介绍了获取片段软键盘高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含添加个人信息20+行的片段。
行的确切数目取决于的雇主从应用程序的用户有多少希望。

I have a fragment that contains 20+ rows for adding personal info. The exact number of rows depends of how much an employer wants from the users of the app.

如果我需要显示的行数大于可用空间大,具有软键盘的表现,我必须将其他行移动到下一个屏幕,等等。

If the number of rows I need to show is bigger than the available space, with soft keyboard showing, I have to move the other rows to the next screen, and so on.

这是我Gameplan的:
检查我行有多高利润+(简单,难codeD)
检查总的屏幕高度
检查软键盘高度

This was my gameplan: Check how high my rows are + margins (easy, hardcoded) Check the total screen height Check the soft keyboard height

这会给我足够的信息来计算...你会得到的图片:)

That would give me enough info to calculate... you'll get the picture :)

有似乎没有像getSoftKeyboardHeight,所以我在片段用这个code:

There didn't seem to be something like "getSoftKeyboardHeight", so I used this code in my fragment:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        root = getActivity().getWindow().getDecorView().findViewById(R.id.fragment_container);

        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                int heightDiff = root.getRootView().getHeight() - root.getHeight();
                // IF height diff is more then 150, consider keyboard as visible.
                Log.d("slideactivity", "total " + root.getRootView().getHeight());
                Log.d("slideactivity", "total " + root.getHeight());
                Log.d("slideactivity", "heightdiff = " + heightDiff);
            }
        });
    }

日志输出:

08-13 12:37:55.242     D/slideactivity﹕ total 600
08-13 12:37:55.242     D/slideactivity﹕ total 543
08-13 12:37:55.242     D/slideactivity﹕ heightdiff = 57

虽然实际上我一半的屏幕是采取了由软键盘。
我开始怀疑..也许键盘只是弹出时,你得到打印的值,
所以我没有得到正确的读..
我用了一个哈克的Thread.Sleep在异步任务测量前,但同样的信息上来。

While actually half my screen is taken up by the soft keyboard. I started to wonder.. maybe the keyboard is just popping up when the values get printed, so I didn't get a correct read.. I used a hacky Thread.Sleep in an async task before measuring, but the same info came up.

有谁知道如何解决这个问题?
也许有一些库,做所有的工作?
或者,我在找错了地方的解决方案?

Does anyone know how to fix this problem? Maybe there is some library that does all the work? Or I'm looking for solutions in the wrong places?

任何帮助非常多AP preciated,因为我已经查了很多计算器的答案!

Any help is very much appreciated, since I already checked a lot of stackoverflow answers!

推荐答案

我终于找到了解决办法。
为什么没有发挥出来的原因是因为我没有把我的滚动型元素的含量。
通过这样做,softkeyboard推布局,因此产生的高度差,可以测量

I finally found a solution. The reason why this didn't work out is because I didn't place my content in a ScrollView element. By doing this, the softkeyboard pushes the layout, hence creating a difference in height, that can be measured.

这篇关于获取片段软键盘高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆