Android How to adjust layout in Full Screen Mode when softkeyboard is visible

前端 未结 25 1788
后悔当初
后悔当初 2020-11-22 03:56

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme=\"@andro

25条回答
  •  攒了一身酷
    2020-11-22 04:35

    To get it to work with FullScreen:

    Use the ionic keyboard plugin. This allows you to listen for when the keyboard appears and disappears.

    OnDeviceReady add these event listeners:

    // Allow Screen to Move Up when Keyboard is Present
    window.addEventListener('native.keyboardshow', onKeyboardShow);
    // Reset Screen after Keyboard hides
    window.addEventListener('native.keyboardhide', onKeyboardHide);
    

    The Logic:

    function onKeyboardShow(e) {
        // Get Focused Element
        var thisElement = $(':focus');
        // Get input size
        var i = thisElement.height();
        // Get Window Height
        var h = $(window).height()
        // Get Keyboard Height
        var kH = e.keyboardHeight
        // Get Focused Element Top Offset
        var eH = thisElement.offset().top;
        // Top of Input should still be visible (30 = Fixed Header)
        var vS = h - kH;
        i = i > vS ? (vS - 30) : i;
        // Get Difference
        var diff = (vS - eH - i);
        if (diff < 0) {
            var parent = $('.myOuter-xs.myOuter-md');
            // Add Padding
            var marginTop = parseInt(parent.css('marginTop')) + diff - 25;
            parent.css('marginTop', marginTop + 'px');
        }
    }
    
    function onKeyboardHide(e) {
      // Remove All Style Attributes from Parent Div
      $('.myOuter-xs.myOuter-md').removeAttr('style');
    }
    

    Basically if they difference is minus then that is the amount of pixels that the keyboard is covering of your input. So if you adjust your parent div by this that should counteract it.

    Adding timeouts to the logic say 300ms should also optimise performance (as this will allow keyboard time to appear.

提交回复
热议问题