跳到主要内容
版本:v3

@capacitor/keyboard

Keyboard API 提供键盘显示和可见性控制,以及键盘显示和隐藏时的事件跟踪。

安装

npm install @capacitor/keyboard
npx cap sync

示例

import { Keyboard } from '@capacitor/keyboard';

Keyboard.addListener('keyboardWillShow', info => {
console.log('keyboard will show with height:', info.keyboardHeight);
});

Keyboard.addListener('keyboardDidShow', info => {
console.log('keyboard did show with height:', info.keyboardHeight);
});

Keyboard.addListener('keyboardWillHide', () => {
console.log('keyboard will hide');
});

Keyboard.addListener('keyboardDidHide', () => {
console.log('keyboard did hide');
});

配置

在 iOS 上,可以通过以下选项配置键盘:

属性Type描述默认值始于
resize
KeyboardResize
配置键盘出现时应用调整大小的方式。仅在 iOS 上可用。native1.0.0
style'dark' | 'light'如果您的应用不支持暗/亮主题切换,则覆盖键盘样式。如果未设置,键盘样式将取决于设备外观。仅在 iOS 上可用。1.0.0
resizeOnFullScreenboolean存在一个 Android 错误,当应用处于全屏模式时(例如使用 StatusBar 插件覆盖状态栏),键盘无法调整 WebView 大小。如果此设置设为 true,则添加一种解决方法,即使在应用全屏时也能调整 WebView 大小。仅适用于 Android。1.1.0

示例

capacitor.config.json 中:

{
"plugins": {
"Keyboard": {
"resize": "body",
"style": "dark",
"resizeOnFullScreen": true
}
}
}

capacitor.config.ts 中:

/// <reference types="@capacitor/keyboard" />

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
plugins: {
Keyboard: {
resize: "body",
style: "dark",
resizeOnFullScreen: true,
},
},
};

export default config;

cordova-plugin-ionic-keyboard 的兼容性

为了保持与 cordova-plugin-ionic-keyboard 的兼容性,以下事件也可与 window.addEventListener 一起使用:

  • keyboardWillShow
  • keyboardDidShow
  • keyboardWillHide
  • keyboardDidHide

API

show()

show() => Promise<void>

显示键盘。此方法处于 alpha 阶段,可能存在一些问题。

此方法仅在 Android 上受支持。

始于: 1.0.0


hide()

hide() => Promise<void>

隐藏键盘。

始于: 1.0.0


setAccessoryBarVisible(...)

setAccessoryBarVisible(options: { isVisible: boolean; }) => Promise<void>

设置键盘上的辅助工具栏是否可见。我们建议对短表单(登录、注册等)禁用辅助工具栏,以提供更简洁的 UI。

此方法仅在 iPhone 设备上受支持。

参数Type
options{ isVisible: boolean; }

始于: 1.0.0


setScroll(...)

setScroll(options: { isDisabled: boolean; }) => Promise<void>

以编程方式启用或禁用 WebView 滚动。

此方法仅在 iOS 上受支持。

参数Type
options{ isDisabled: boolean; }

始于: 1.0.0


setStyle(...)

setStyle(options: KeyboardStyleOptions) => Promise<void>

以编程方式设置键盘样式。

此方法仅在 iOS 上受支持。

参数Type
options
KeyboardStyleOptions

始于: 1.0.0


setResizeMode(...)

setResizeMode(options: KeyboardResizeOptions) => Promise<void>

以编程方式设置调整大小模式。

此方法仅在 iOS 上受支持。

参数Type
options
KeyboardResizeOptions

始于: 1.0.0


addListener('keyboardWillShow', ...)

addListener(eventName: 'keyboardWillShow', listenerFunc: (info: KeyboardInfo) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

监听键盘即将显示的事件。

参数Type
eventName'keyboardWillShow'
listenerFunc
(info: KeyboardInfo) => void

返回:

Promise<PluginListenerHandle> & PluginListenerHandle

始于: 1.0.0


addListener('keyboardDidShow', ...)

addListener(eventName: 'keyboardDidShow', listenerFunc: (info: KeyboardInfo) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

监听键盘已显示的事件。

参数Type
eventName'keyboardDidShow'
listenerFunc
(info: KeyboardInfo) => void

返回:

Promise<PluginListenerHandle> & PluginListenerHandle

始于: 1.0.0


addListener('keyboardWillHide', ...)

addListener(eventName: 'keyboardWillHide', listenerFunc: () => void) => Promise<PluginListenerHandle> & PluginListenerHandle

监听键盘即将隐藏的事件。

参数Type
eventName'keyboardWillHide'
listenerFunc() => void

返回:

Promise<PluginListenerHandle> & PluginListenerHandle

始于: 1.0.0


addListener('keyboardDidHide', ...)

addListener(eventName: 'keyboardDidHide', listenerFunc: () => void) => Promise<PluginListenerHandle> & PluginListenerHandle

监听键盘已隐藏的事件。

参数Type
eventName'keyboardDidHide'
listenerFunc() => void

返回:

Promise<PluginListenerHandle> & PluginListenerHandle

始于: 1.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

移除该插件的所有原生监听器。

始于: 1.0.0


接口

KeyboardStyleOptions

属性Type描述默认值始于
style
KeyboardStyle
键盘样式。KeyboardStyle.Default1.0.0

KeyboardResizeOptions

属性Type描述始于
mode
KeyboardResize
键盘出现时用于调整元素大小的模式。1.0.0

PluginListenerHandle

属性Type
remove() => Promise<void>

KeyboardInfo

属性Type描述始于
keyboardHeightnumber键盘高度。1.0.0

枚举

KeyboardStyle

成员Value描述始于
Dark'DARK'深色键盘。1.0.0
Light'LIGHT'浅色键盘。1.0.0
Default'DEFAULT'在 iOS 13 及更高版本上,键盘样式基于设备外观。如果设备使用深色模式,键盘将为深色。如果设备使用浅色模式,键盘将为浅色。在 iOS 12 上,键盘将为浅色。1.0.0

KeyboardResize

成员Value描述始于
Body'body'仅调整 body HTML 元素的大小。相对单位不受影响,因为视口不会改变。1.0.0
Ionic'ionic'仅调整 ion-app HTML 元素的大小。仅适用于 Ionic Framework 应用。1.0.0
Native'native'键盘显示/隐藏时,整个原生 Web View 将被调整大小。这会影响 vh 相对单位。1.0.0
None'none'应用和 Web View 都不会被调整大小。1.0.0