ReactNative之键盘Keyboard的弹出与消失示例

网友投稿 528 2023-04-28


ReactNative之键盘Keyboard的弹出与消失示例

在开发中经常遇到需要输入的地方,所以就学习了一下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-nativeVKgzPw';

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 (

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`

- VKgzPw`keyboardWillChangeFrame`

- `keyboardDidChangeFrame`

使用的时候需要测试下android和iOS下监听的事件是否都ok。

踩坑如下:

android 对keyboardWillShow 监听不到。

同样,我们在源码里可以找到使键盘消失的函数

/**

* Dismisses the active keyboard and removes focus.

*/

dismiss () {

dismissKeyboard();

}

我们如果需要使用时,可以如下:

const dismissKeyboard = require('dismissKeyboard');

dismissKeyboard();

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`

- VKgzPw`keyboardWillChangeFrame`

- `keyboardDidChangeFrame`

使用的时候需要测试下android和iOS下监听的事件是否都ok。

踩坑如下:

android 对keyboardWillShow 监听不到。

同样,我们在源码里可以找到使键盘消失的函数

/**

* Dismisses the active keyboard and removes focus.

*/

dismiss () {

dismissKeyboard();

}

我们如果需要使用时,可以如下:

const dismissKeyboard = require('dismissKeyboard');

dismissKeyboard();


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:实现继承 接口继承(继承的接口之间使用什么隔开)
下一篇:MyBatis动态SQL标签用法实例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~