简体   繁体   English

当软键盘出现在phonegap中时隐藏输入字段

[英]input field hidden when soft keyboard appears in phonegap

Creating a mobile application using Phonegap 3.6.3 for Android and iOS. 使用适用于Android和iOS的Phonegap 3.6.3创建移动应用程序。 The problem is only for Android, as iOS acts as I would like it to. 问题仅适用于Android,因为iOS会按照我的意愿行事。

When I click on an input text field, or a textarea, a soft keyboard appears. 当我单击输入文本字段或textarea时,会出现一个软键盘。 It covers these elements sometimes. 它有时涵盖这些元素。

The pages are placed within a iScroll, right at the bottom, and another absolute-placed div, thus I cannot scroll to either of these once the screen pops up. 这些页面放在iScroll中,位于底部,另一个是绝对放置的div,因此一旦屏幕弹出,我就无法滚动到其中任何一个。 I suspect I have to change the webview to be smaller when the keyboard comes up. 我怀疑当键盘出现时我必须将webview更改为更小。 However, after trying many things, it isn't working. 然而,在尝试了很多东西后,它无法正常工作。

config.xml config.xml中

    <preference name="permissions" value="none" />
    <preference name="phonegap-version" value="3.5.0" />
    <preference name="orientation" value="default" />
    <preference name="target-device" value="universal" />
    <preference name="fullscreen" value="false" />
    <preference name="webviewbounce" value="true" />
    <preference name="prerendered-icon" value="false" />
    <preference name="stay-in-webview" value="false" />
    <preference name="ios-statusbarstyle" value="black-opaque" />
    <preference name="detect-data-types" value="true" />
    <preference name="exit-on-suspend" value="false" />
    <preference name="show-splash-screen-spinner" value="false" />
    <preference name="auto-hide-splash-screen" value="false" />
    <preference name="disable-cursor" value="false" />
    <preference name="android-minSdkVersion" value="13" />
    <preference name="android-windowSoftInputMode" value="adjustResize|stateHidden" />
    <preference name="android-installLocation" value="auto" />
    <preference name="SplashScreen" value="splash" />

(have tried many different values for android-windowSoftInputMode, as per example below) (已经为android-windowSoftInputMode尝试了许多不同的值,如下例所示)

    <preference name="android-windowSoftInputMode" value="stateVisible|adjustResize|adjustPan" />

head of web page 网页的负责人

    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" />

(yes, I've tried many other things too) (是的,我也尝试过很多其他的东西)

anything else you may perceive as relevant, please let me know. 您可能认为相关的任何其他内容,请告诉我。 Unfortunately, I'm not at liberty to share too much of the code, but from my understanding that's all we need to worry about. 不幸的是,我不能自由地分享太多的代码,但根据我的理解,我们需要担心的是。

Thanks, 谢谢,

I too had the same problem - the underlying problem it seems is you can't edit the androidManifest file with cordova Build. 我也有同样的问题 - 看起来潜在的问题是你不能用cordova Build编辑androidManifest文件。 All you can do is edit the config.xml file which really only allows you to change a limited subset of settings. 您所能做的就是编辑config.xml文件,该文件实际上只允许您更改有限的设置子集。 What I would like is to be able to change the windowSoftInputMode . 我想要的是能够更改windowSoftInputMode

I did find a solution to my problem. 我确实找到了解决问题的方法。 which was the keyboard appearing over fields at the bottom of the screen which I think is the same problem you're having. 这是键盘出现在屏幕底部的字段上,我认为这是你遇到的同样问题。 I have used the cordova 2.9.0 and cordova 3.6.0 我使用过cordova 2.9.0和cordova 3.6.0

Solution 1: The solution I found was to change this setting in config.xml 解决方案1:我发现的解决方案是在config.xml更改此设置

<preference name="fullscreen" value="false" />

This does with that setting set to "false" instead of "true" - the page now scrolls up to reveal the field you're editing when the keyboard opens. 这样做的设置设置为“false”而不是“true” - 页面现在向上滚动以显示键盘打开时正在编辑的字段。 (to be more accurate, I believe the viewport changes rather than scroll up. Hope you are right on your viewport part) (为了更准确,我相信viewport改变而不是向上滚动。希望你在视口部分是正确的)

Solution 2: Try remove or replace this line from config.xml 解决方案2:尝试从config.xml中删除或替换此行

<preference name="android-windowSoftInputMode" value="adjustResize|stateHidden" />

as

<preference name="android-windowSoftInputMode" value="stateVisible|adjustResize"/>

instead. 代替。 This works for me now. 这对我来说很有用。

Solution 3: Try adding this style to your page 解决方案3:尝试将此样式添加到您的页面

<style> <item name="android:windowTranslucentStatus">true</item></style>

EDIT: 编辑:

If your are using jQuery you can try that. 如果你正在使用jQuery你可以试试。

$('input, textarea, button, a, select').off('touchstart mousedown').on('touchstart mousedown', function(e) {
    e.stopPropagation();
});

My solution was using Ionic keyboard plugin and implementing this code: 我的解决方案是使用Ionic键盘插件并实现此代码:

HTML: HTML:

<div id="page" class="page-content">
    ...         
</div> 

CSS: CSS:

.page-content {    
height: 100%;
overflow: auto;}

Javascript: 使用Javascript:

  • when keyborad is open 当keyborad打开时

      window.addEventListener('native.keyboardshow', function (e) { var deviceHeight = window.innerHeight; var keyboardHeight = e.keyboardHeight; var deviceHeightAdjusted = deviceHeight - keyboardHeight;//device height adjusted deviceHeightAdjusted = deviceHeightAdjusted < 0 ? (deviceHeightAdjusted * -1) : deviceHeightAdjusted;//only positive number document.getElementById('page').style.height = deviceHeightAdjusted + 'px';//set page height document.getElementById('page').setAttribute('keyBoardHeight', keyboardHeight);//save keyboard height }); 
  • when keyboard is closed 键盘关闭时

      window.addEventListener('native.keyboardhide', function (e) { setTimeout(function () { document.getElementById('page').style.height = 100 + '%';//device 100% height }, 100); 

To provide a better user experience, add this code for all inputs 要提供更好的用户体验,请为所有输入添加此代码

var inputs = document.querySelectorAll('input');//select all input
var n = inputs.length;
for (var i = 0; i < n; i++) {
    var input = inputs[i];
    input.addEventListener('focus', function (e) {//add event focus
        var inp = this; 
       //wait 0.6 seconds to scroll down to the input has focus
        setTimeout(function () {
            try {
                //if the keyboard is open
                if (cordova.plugins.Keyboard.isVisible) {
                    var padding = 15;
                    var targetPosition = parseInt($$(inp).offset().top + padding);
                    var keyboardHeight = parseInt(document.getElementById('page').getAttribute('keyBoardHeight'));//get keyboard height   

                    //if the input is hidden by the keyboard,scroll to the input 
                    if (targetPosition >= keyboardHeight) {
                        padding *=5;
                        document.getElementById('page').scrollTop = targetPosition - padding;
                    }
                }
            } catch (ex) {
                alert(ex.message);
            }
        }, 600);
    }, true);

Remove this line from config.xml 从config.xml中删除此行

<preference name="android-windowSoftInputMode" value="adjustResize|stateHidden" />

And change the line in 'head of web page' to 并将“网页头部”中的行更改为

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />

Esta funciona muy bien. Esta funciona muy bien。

config.xml config.xml中

plugin name="com.ionic-for-phonegap.keyboard" spec="0.0.1" source="pgb" 插件名称=“com.ionic-for-phonegap.keyboard”spec =“0.0.1”source =“pgb”

  window.addEventListener('native.hidekeyboard', keyboardHideHandler); window.addEventListener('native.showkeyboard', keyboardShowHandler); function keyboardHideHandler(e){ alert('Goodnight, sweet prince'); } function keyboardShowHandler(e){ alert('Keyboard height is: ' + e.keyboardHeight); } 

I have the most efficient solution to scroll into input automatically and make it visible. 我有最有效的解决方案来自动滚动输入并使其可见。 First you need to add the keyboard plugin(cordova plugin add com.ionic.keyboard) as mentioned by @Fedir. 首先,你需要添加@Fedir所提到的键盘插件(cordova插件添加com.ionic.keyboard)。 Then on your event handler of 'keyboardshow' event add the following code: 然后在'keyboardshow'事件的事件处理程序中添加以下代码:

window.addEventListener('native.keyboardshow', function(e){ 
    setTimeout(function() {
        document.activeElement.scrollIntoViewIfNeeded();
    }, 100);
});

PS: This is supported only on Android (Chrome) and Safari. PS:仅在Android(Chrome)和Safari上支持此功能。 :D :d

Only one solution which worked for my full-screen app, it's to add to the Cordova application ionic-plugin-keyboard plugin 只有一个解决方案适用于我的全屏应用程序,它是添加到Cordova应用程序的离子插件键盘插件

cordova plugin add com.ionic.keyboard

JS code : JS代码:

// This event fires when the keyboard will hide
window.addEventListener('native.keyboardshow', keyboardShowHandler);

function keyboardShowHandler(e){
    $("body").addClass("keyboardOn");
}

// This event fires when the keyboard will show
window.addEventListener('native.keyboardhide', keyboardHideHandler);

function keyboardHideHandler(e){
    $("body").removeClass("keyboardOn");
}

After that You could adjust the view with CSS. 之后你可以用CSS调整视图。

ref.: https://stackoverflow.com/a/25823491/634275 参考: https//stackoverflow.com/a/25823491/634275

My App uses the Android fullscreen / immersive mode. 我的应用程序使用Android全屏/沉浸式模式。 I have solved it by switching the the modes as soon as the keyboard appears/disappears. 一旦键盘出现/消失,我就通过切换模式解决了这个问题。

I have installed the cordova-plugin-fullscreen and ionic-plugin-keyboard plugins and added this code: 我已经安装了cordova-plugin-fullscreen和ionic-plugin-keyboard插件并添加了以下代码:

document.addEventListener('deviceready', 
  function(){
    // disable immersive mode  on Android when keyboard is shown
        try {
      if (cordova.platformId == 'android') {
        AndroidFullScreen.immersiveMode(false, false);
        window.addEventListener('native.keyboardshow', function (e) {
          AndroidFullScreen.showSystemUI(false, false);

        });
        window.addEventListener('native.keyboardhide', function (e) {
          AndroidFullScreen.immersiveMode(false, false);
        });
      }
    } catch (error) {
      console.log('deviceready - ' + error);
    }
}, false);

I am using the Ionic Keyboard Plugin to detect when the keyboard is open and add necessary padding accordingly to the body: 我正在使用Ionic Keyboard Plugin来检测键盘何时打开并根据身体添加必要的填充:

phonegap plugin add ionic-plugin-keyboard --save

When the events fire, you can adjust the padding-bottom on the body to match the height of the keyboard. 当事件触发时,您可以调整主体上的padding-bottom以匹配键盘的高度。

Then, in order to detect if the focused element is visible, you have to do the math and scroll the container if necessary. 然后,为了检测聚焦元素是否可见,您必须进行数学运算并在必要时滚动容器。 The final code I am using is the following: 我使用的最终代码如下:

cordova.plugins.Keyboard.disableScroll(true);
$$(window).on('native.keyboardshow', function(e) {
    $$('body').css('padding-bottom', e.keyboardHeight + 'px');

    var el = $$('input:focus, textarea:focus');
    var content = el.closest('.page-content');
    var offset = el.offset().top + content.scrollTop();
    /* 15 is an arbitrary value to add a bit of padding */
    var safeAreaHeight = el.outerHeight(true) + 15;
    var maxPosition = content.height() - safeAreaHeight;
    if (content.scrollTop() < (offset - maxPosition)) {
            content.scrollTop(offset - maxPosition);
    }

});
$$(window).on('native.keyboardhide', function(e) {
    $$('body').css('padding-bottom', 0);
});

The code makes use of Framework7 and its Dom7 library which is very similar to jQuery. 该代码使用了Framework7及其与jQuery非常相似的Dom7库。 It assumes the scrollable element is .page-content , but you can adjust it accordingly. 它假设可滚动元素是.page-content ,但您可以相应地调整它。

You should leave the android-windowSoftInputMode preference at its default value. 您应该将android-windowSoftInputMode首选项保留为其默认值。

只需在config.xml文件中添加以下行即可...

<preference name="android-windowSoftInputMode" value="stateVisible|adjustResize"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM