跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >Version: 0.71

Keyboard

Keyboard模块用来控制键盘相关的事件。

用法示例

Keyboard模块可以监听原生键盘事件以做出相应回应,比如收回键盘。


文档

方法

addListener()

jsx
static addListener(eventName, callback)

addListener用于注册一个 JavaScript 函数来监听处理原生键盘通知事件。

此方法会返回监听函数的引用。

参数:

名称类型Required说明
eventNamestringYesnativeEvent参数用来指明要监听的事件,具体有以下几种:
callbackfunctionYes事件触发时调用的 js 函数

nativeEvent

This can be any of the following

  • keyboardWillShow
  • keyboardDidShow
  • keyboardWillHide
  • keyboardDidHide
  • keyboardWillChangeFrame
  • keyboardDidChangeFrame

keyboardWillShow as well as keyboardWillHide are generally not available on Android since there is no native corresponding event.

注意在 Android 上只有keyboardDidShowkeyboardDidHide事件有效。如果 Android 版本小于等于 10, 且android:windowSoftInputMode设置为adjustNothing,则没有任何事件有效。


removeListener()

jsx
static removeListener(eventName, callback)

移除某个类型事件的监听函数。

参数:

名称类型Required说明
eventNamestringYes要移除监听函数的原生事件类型名称
callbackfunctionYes要移除的监听函数

removeAllListeners()

jsx
static removeAllListeners(eventName)

移除某个类型事件的所有监听函数。


dismiss()

jsx
static dismiss()

把弹出的键盘收回去,同时使当前的文本框失去焦点。


scheduleLayoutAnimation

jsx
static scheduleLayoutAnimation(event)

Useful for syncing TextInput (or other keyboard accessory view) size of position changes with keyboard movements.