简体   繁体   English

键盘出现时输入文本失去焦点 - iOS PhoneGap 应用程序

[英]lost focus of input text while keyboard appears - iOS PhoneGap Application

i have an issue while developing phone gap application on iOS 7 using cordova 2.7 with html input text.我在使用cordova 2.7和html输入文本在iOS 7上开发电话间隙应用程序时遇到问题。 when i select input text the keyboard pops up.当我选择输入文本时,键盘会弹出。 but can't type anything as the focus is lost.但无法输入任何内容,因为焦点已丢失。 i have to select again to enter text.我必须再次选择才能输入文本。

can anyone help me on this.谁可以帮我这个事。

I ran into a similar issue where the keyboard would come up, but nothing typed shows up in the textbox.我遇到了类似的问题,键盘会出现,但文本框中没有显示任何输入内容。 Mine was caused by css -我的是由 css 引起的 -

* {
  -webkit-user-select: none; /* prevent copy paste */
}

I fixed the issue by overriding the style for textboxes -我通过覆盖文本框的样式解决了这个问题 -

input[type="text"] {
    -webkit-user-select: text;
}

There is a config file inside cordova apps, config.xml where by default cordova does not allow you to control focus from javascript calls, this means that the keyboard can "disappear" cordova 应用程序中有一个配置文件 config.xml,默认情况下,cordova 不允许您通过 javascript 调用控制焦点,这意味着键盘可以“消失”

Change this:改变这个:

<preference name="KeyboardDisplayRequiresUserAction" value="true" />

to

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

and then just write an event handler for the field where it sets focus on itself when tapped inside a setTimeout.然后只需为该字段编写一个事件处理程序,当在 setTimeout 中点击时,它会将焦点设置在自身上。 This worked really well for me recently.这最近对我来说非常有效。

This is a known issue which has already been logged with Cordova here: https://issues.apache.org/jira/browse/CB-5115 .这是一个已知问题,已在 Cordova 中记录: https : //issues.apache.org/jira/browse/CB-5115 I would also like a workaround to this as it's not ideal.我也想解决这个问题,因为它并不理想。

Here is the workaround as explained there,这是那里解释的解决方法,

window.document.body.ontouchstart = (e) => {
  if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
    e.preventDefault();
    e.target.focus();
  }
};

I ran into this issue and found out I had fixed it in another Phonegap project using this.我遇到了这个问题,发现我已经在另一个 Phonegap 项目中使用它修复了它。 It's basically same as @mld answer, but using html.它与@mld 的答案基本相同,但使用 html。 Using * doesn't work for my app on iOS.使用 * 不适用于我在 iOS 上的应用。

html {
  -webkit-user-select: none; /* prevent text selection */
}

input[type="text"] {
    -webkit-user-select: text;
}

I had this issue in an Ionic V1 / Angular 1.5 project.我在 Ionic V1 / Angular 1.5 项目中遇到了这个问题。 This fix worked for me:这个修复对我有用:

window.addEventListener('native.keyboardshow', function () {
  if ( document.activeElement != document.getElementById('my-input') && document.activeElement.nodeName != 'INPUT' ){
    document.getElementById('my-input').focus()
  }
});

When we tap the input, the keyboard comes up.当我们点击输入时,键盘就会出现。 We can then check if our input element is actually focused.然后我们可以检查我们的输入元素是否真的被聚焦。 If not, we manually focus it.如果没有,我们手动对焦。 If it's another input, we won't focus it.如果它是另一个输入,我们将不会关注它。

I called this inside my component's $onInit function - make sure to remove the event listener when your component is destroyed with $onDestroy .我在我的组件的$onInit函数中调用了它 - 当你的组件被$onDestroy销毁时,确保移除事件监听器。 This also assumes you're using the ionic-plugin-keyboard plugin.这也假设您正在使用ionic-plugin-keyboard插件。

This works well with one input, but if you have multiple inputs on the same page, you will probably need additional logic to prevent your app from focusing on the wrong input when the keyboard opens.这适用于一个输入,但如果您在同一页面上有多个输入,您可能需要额外的逻辑来防止您的应用程序在键盘打开时专注于错误的输入。

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

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