我正在寻找一个简单的解决方案,以检测移动设备上的键盘是否已打开/关闭(堆栈:Ionic2,Angular2)。
Ionic是否会传播任何键盘 - 打开'或者'键盘关闭'上课进入body / html?
答案 0 :(得分:2)
键盘是移动设备的原生。因此,您需要使用本机插件来检查其功能。安装cordova插件和离子原生类型如下
ionic plugin add ionic-plugin-keyboard
npm install --save @ionic-native/keyboard
添加以下代码行以检查键盘是否打开和关闭
import { Keyboard } from '@ionic-native/keyboard';
constructor(private keyboard: Keyboard) {
...
//Observes when the keyboard is shown
this.keyboard.onKeyboardShow();
//Observes when the keyboard is hidden
this.keyboard.onKeyboardHide();
}
答案 1 :(得分:1)
离子不会发出keyboard-open
或keyboard-close
,但有ionic-plugin-keyboard就是这样。它会触发native.keyboardshow
和native.keyboardhide
个事件。您还可以查询Keyboard.isVisible
属性。
答案 2 :(得分:0)
丑陋的答案,但就像魅力一样:
import {NgZone} from '@angular/core';
public IsKeyboardOpen:boolean=false;
constructor(public ngZ:NgZone)
{
var innerHeight=window.innerHeight;
window.onresize = (e) =>
{
this.ngZ.run(() =>
{
if(window.innerHeight< innerHeight)
{
this.IsKeyboardOpen=true;
}
else
{
this.IsKeyboardOpen=false;
}
});
};
}
答案 3 :(得分:0)
尝试使用此插件:https://ionicframework.com/docs/native/keyboard/
window.addEventListener('keyboardWillShow', (event) => {
// Describe your logic which will be run each time when keyboard is about to be shown.
console.log(event.keyboardHeight);
});