Keyboard组件可以用来控制键盘相关的事件。

用法

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

import React, { Component } from 'react';
import { Keyboard, TextInput } from 'react-native';

class Example extends Component {
  componentWillMount () {
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
  }

  componentWillUnmount () {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
  }

  _keyboardDidShow () {
    alert('Keyboard Shown');
  }

  _keyboardDidHide () {
    alert('Keyboard Hidden');
  }

  render() {
    return (
      <TextInput
        onSubmitEditing={Keyboard.dismiss}
      />
    );
  }
}

方法

static addListener(nativeEvent, jsFunction) #

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

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

@param {string} nativeEvent nativeEvent参数用来指明要监听的事件,具体有以下几种:

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

注意如果你把`android:windowSoftInputMode`设置为`adjustResize`或是`adjustNothing`,则在Android上只有`keyboardDidShow`和`keyboardDidHide`事件有效。

>

@param {function} jsFunction 事件触发时调用的js函数。

static removeAllListeners(eventType) #

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

@param {string} eventType 要移除的原生事件类型

static removeSubscription(subscription) #

@param {EmitterSubscription} subscription 要移除的监听函数

static dismiss() #

把弹出的键盘收回去。


书籍推荐