跳至主内容
版本:0.80

键盘

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

Keyboard 模块用于控制键盘事件。

使用示例

Keyboard 模块允许您监听原生事件并作出响应,还可以对键盘进行更改(例如将其关闭)。


参考

方法

addListener()

tsx
static addListener: (
eventType: KeyboardEventName,
listener: KeyboardEventListener,
) => EmitterSubscription;

addListener 函数用于将 JavaScript 函数连接到指定的原生键盘通知事件。

该函数会返回监听器的引用。

参数:

NameTypeDescription
eventName
Required
stringThe string that identifies the event you're listening for. See the list below.
callback
Required
functionThe function to be called when the event fires

eventName

可以是以下任意事件:

  • keyboardWillShow

  • keyboardDidShow

  • keyboardWillHide

  • keyboardDidHide

  • keyboardWillChangeFrame

  • keyboardDidChangeFrame

注意:在 Android 上仅支持 keyboardDidShowkeyboardDidHide 事件。如果您的 activity 将 android:windowSoftInputMode 设为 adjustNothing,则 Android 10 及以下系统不会触发这些事件。


dismiss()

tsx
static dismiss();

关闭当前键盘并移除焦点。


scheduleLayoutAnimation

tsx
static scheduleLayoutAnimation(event: KeyboardEvent);

用于同步 TextInput(或其他键盘辅助视图)的尺寸或位置变化与键盘移动。


isVisible()

tsx
static isVisible(): boolean;

获取键盘最后已知的可见状态。


metrics()

tsx
static metrics(): KeyboardMetrics | undefined;

返回当前可见软键盘的尺寸信息。