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

Javascript 如何在Android浏览器上检测键盘显示/隐藏事件的发生,javascript,android,Javascript,Android,我陷入困境。我无法确定在移动设备浏览器上捕获键盘显示/隐藏状态的方法 问题: 我在表单上有一个弹出窗口,其中有一个文本字段。当用户点击文本字段时,键盘就会出现,按下表单上的弹出窗口,最终使文本字段不可见 有没有办法识别键盘的显示/隐藏状态 不,无法可靠地知道键盘何时显示。你所拥有的一个控制级别是,当键盘显示时,你可以将应用程序设置为平移或调整大小。如果您将其设置为“调整大小”,它将重新计算布局并缩小内容,以便与剩余屏幕相匹配。如果选择“平移”,它将保持相同的大小,只需向上滑动整个应用程序。您可以

我陷入困境。我无法确定在移动设备浏览器上捕获键盘显示/隐藏状态的方法

问题:

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


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

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

您可以在应用程序中找到键盘显示/隐藏,在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);
                                    }
                            }

                    }
            });

在浏览器中似乎没有可靠的方法可以做到这一点。我最近听到的是关注事件,然后暂时收听调整大小事件。如果在接下来的<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); 
       }
    });
onDocumentReady=函数(){
var$document=$(文档);
变量$window=$(window);
var initialHeight=window.outerHeight;
var currentHeight=初始高度;
//听所有未来的文本输入
//如果是焦点,请倾听调整大小的声音。
$document.on(“焦点.键盘”,“输入[type='text'],textarea”,函数(事件){
//如果之后立即调整大小,则假定键盘处于打开状态。
$window.on(“resize.keyboard”,function()){
$window.off(“resize.keyboard”);
currentHeight=window.outerHeight;
如果(当前高度<初始高度){
window.isKeyboardIn=true;
}
});
//只听半秒钟。
setTimeout($window.off.bind($window,“resize.keyboard”),500);
});
//在模糊时,检查屏幕是否已恢复正常
$document.on(“blur.keyboard”,“input[type=“text”],textarea”,function()){
if(window.isKeyboardIn){
setTimeout(函数(){
currentHeight=window.outerHeight;
如果(当前高度===初始高度){
window.isKeyboardIn=false;
}, 500); 
}
});

}

我要求在浏览器中进行事件检测。所以应该是在Javascript中。