在开发中经常遇到需要输入的地方,所以就学习了一下ReactNative键盘Keyboard的弹出与消失的方法,留个笔记。
在React-native 的Component组件中有个Keyboard.
我们先来看下官方提供的例子,监听键盘的弹出与消失。Demo如下:
class Example extends Component {
componentWillMount () {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow',this._keyboardDidShow);
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide',this._keyboardDidHide);
}
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}
/>
);
}
}
Keyboard支持的监听事件如下:
使用的时候需要测试下Android和iOS下监听的事件是否都ok。
踩坑如下:
android 对keyboardWillShow 监听不到。
我们如果需要使用时,可以如下: