这篇文章主要介绍了ReactNative之键盘Keyboard弹出与消失的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
RN对键盘事件的支持。
在React-native 的Component组件中有个Keyboard.
github地址如下:https://github.com/facebook/react-native/tree/770091f3c13f7c1bd77c50f979d89a774001fbf0/Libraries/Components/Keyboard
我们先来看下官方提供的例子,监听键盘的弹出与消失。Demo如下:
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} /> ); } }
Keyboard支持的监听事件如下:
@param {string} nativeEvent The `nativeEvent` is the string that identifies the event you're listening for. This can be any of the following: - `keyboardWillShow` - `keyboardDidShow` - `keyboardWillHide` - `keyboardDidHide` - `keyboardWillChangeFrame` - `keyboardDidChangeFrame`
使用的时候需要测试下Android和iOS下监听的事件是否都ok。
踩坑如下:
android 对keyboardWillShow 监听不到。
同样,我们在源码里可以找到使键盘消失的函数
/** * Dismisses the active keyboard and removes focus. */ dismiss () { dismissKeyboard(); }
我们如果需要使用时,可以如下:
const dismissKeyboard = require('dismissKeyboard'); dismissKeyboard();
感谢你能够认真阅读完这篇文章,希望小编分享的“ReactNative之键盘Keyboard弹出与消失的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。