如何在Android浏览器上检测键盘显示/隐藏事件

时间:2014-06-05 06:27:07

标签: javascript android

我正在解决问题。我无法确定在移动设备浏览器上捕获键盘显示/隐藏状态的方法。

问题:

我在表单上有一个弹出文件,其中有一个文本字段。当用户点击文本字段时,键盘会弹出表单上的弹出窗口并最终使文本字段不可见。

有没有办法识别键盘显示/隐藏状态???

3 个答案:

答案 0 :(得分:0)

不,没有办法可靠地知道键盘何时显示。您拥有的一个级别的控制是您可以将应用程序设置为在键盘显示时平移或调整大小。如果将其设置为调整大小,它将重新计算您的布局并缩小内容,以便适合剩余的屏幕。如果您选择平移,它将保持相同的大小,只需向上滑动整个应用程序。

答案 1 :(得分:0)

您可以在应用程序中找到键盘显示/隐藏,在oncreate方法中尝试以下代码,并将您的父布局传递给视图。

final View activityRootView = rellayLoginParent;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
            {
                @Override
                public void onGlobalLayout()
                    {
                        Rect r = new Rect();
                        // r will be populated with the coordinates of your view that area still visible.
                        activityRootView.getWindowVisibleDisplayFrame(r);

                        int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
                        //MyLog.w("height difference is", "" + heightDiff);
                        if (heightDiff > 100)
                            { // if more than 100 pixels, its probably a keyboard...
                                if(lytAppHeader.getVisibility() == View.VISIBLE)
                                    {
                                        lytAppHeader.setVisibility(View.GONE);
                                    }
                            }
                        else
                            {
                                if(lytAppHeader.getVisibility() == View.GONE)
                                    {
                                        lytAppHeader.setVisibility(View.VISIBLE);
                                    }
                            }

                    }
            });

答案 2 :(得分:0)

似乎没有可靠的方法在浏览器中执行此操作。我最接近的是收听焦点事件,然后暂时监听调整大小事件。如果在下一个< 1秒钟,键盘很可能已启动。

为jQuery道歉...

onDocumentReady = function() {
    var $document = $(document);
    var $window = $(window);
    var initialHeight = window.outerHeight;
    var currentHeight = initialHeight;

    // Listen to all future text inputs

    // If it's a focus, listen for a resize.
    $document.on("focus.keyboard", "input[type='text'],textarea", function(event) {

        // If there is a resize immediately after, we assume the keyboard is in.
        $window.on("resize.keyboard", function() {
            $window.off("resize.keyboard");
            currentHeight = window.outerHeight;
            if (currentHeight < initialHeight) {
                window.isKeyboardIn = true;
            }
        });

        // Only listen for half a second.
        setTimeout($window.off.bind($window, "resize.keyboard"), 500);
    });

    // On blur, check whether the screen has returned to normal
    $document.on("blur.keyboard", "input[type="text"],textarea", function() {
       if (window.isKeyboardIn) {
           setTimeout(function() {
               currentHeight = window.outerHeight;
               if (currentHeight === initialHeight) {
                   window.isKeyboardIn = false;
           }, 500); 
       }
    });

};

相关问题