React Native 处理键盘遮挡输入框问题

在移动端上,输入框被软键盘遮挡是一个非常常见的问题,为此,RN原生的提供了一个组件 KeyboardAvoidingView 来处理这个问题,KeyboardAvoidingView的主要属性behavior 包含三个’height’, ‘position’, ‘padding’, 使用最多的就是 padding。但这个组件有时候使用起来可能失效。所以,需要找寻其他的方法

方法一:KeyboardAvoidingView(仔细研究研究,试一试说不定就可以了呢)

方法二:每次输入框聚焦时,获取软件盘的高度,然后让输入框上移;大致代码如下:

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Keyboard,
    TextInput,
    Dimensions
} from 'react-native';
var ScreenWidth = Dimensions.get('window').width;

export default class Root extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            keyboardHeight:0
        };
    }
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                    Welcome to React Native!
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.android.js
                </Text>
                <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{'\n'}
                    Shake or press menu button for dev menu
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.android.js
                </Text>
                <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{'\n'}
                    Shake or press menu button for dev menu
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.android.js
                </Text>
                <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{'\n'}
                    Shake or press menu button for dev menu
                </Text>
                <Text style={styles.instructions}>
                    To get started, edit index.android.js
                </Text>
                <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{'\n'}
                    Shake or press menu button for dev menu
                </Text>
                <TextInput
                    style={{width:ScreenWidth,height:100,borderWidth:2,marginBottom:this.state.keyboardHeight}}
                />
            </View>
        );
    }

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

    componentWillMount() {
        this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this));
        this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this));
    }

    _keyboardDidShow(e){
        this.setState({
            keyboardHeight:e.startCoordinates.height
        })

    }

    _keyboardDidHide(e){
        this.setState({
            keyboardHeight:0
        })
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});

方法三:将 View 组件换成 native-base 的 Content 组件(简单,好用)

 

文章仅为本人学习过程的一个记录,仅供参考,如有问题,欢迎指出!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native 中,当键盘弹出时,会遮挡输入框,这是一个常见的问题。你可以通过以下几种方式来解决: 1. 使用 KeyboardAvoidingView 组件 React Native 提供了 KeyboardAvoidingView 组件,可以自动调整页面的布局,以避免键盘遮挡输入框。你可以将输入框放在 KeyboardAvoidingView 组件中,然后设置 behavior 属性来控制布局的调整方式。例如: ``` import { KeyboardAvoidingView, TextInput } from 'react-native'; <KeyboardAvoidingView behavior={'padding'} style={{ flex: 1 }}> <TextInput style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} /> </KeyboardAvoidingView> ``` 2. 使用 ScrollView 组件 如果页面上有多个输入框,你可以将它们放在一个 ScrollView 组件中,并在键盘弹出时滚动到当前输入框的位置上。你可以使用 scrollTo 方法来实现这个功能。例如: ``` import { ScrollView, TextInput } from 'react-native'; class MyComponent extends React.Component { constructor(props) { super(props); this.state = { inputHeight: 40, }; this.scrollView = React.createRef(); this.textInput = React.createRef(); } componentDidMount() { this.keyboardDidShowListener = Keyboard.addListener( 'keyboardDidShow', this.keyboardDidShow ); this.keyboardDidHideListener = Keyboard.addListener( 'keyboardDidHide', this.keyboardDidHide ); } componentWillUnmount() { this.keyboardDidShowListener.remove(); this.keyboardDidHideListener.remove(); } keyboardDidShow = (event) => { const keyboardHeight = event.endCoordinates.height; const scrollResponder = this.scrollView.current.getScrollResponder(); const inputHandle = ReactNative.findNodeHandle(this.textInput.current); const inputPosition = this.state.inputHeight + 10; scrollResponder.scrollResponderScrollNativeHandleToKeyboard( inputHandle, inputPosition, true ); }; keyboardDidHide = () => { const scrollResponder = this.scrollView.current.getScrollResponder(); scrollResponder.scrollResponderScrollTo({ x: 0, y: 0, animated: true, }); }; render() { return ( <ScrollView ref={this.scrollView} keyboardDismissMode="interactive" contentContainerStyle={{ flexGrow: 1 }} > <TextInput ref={this.textInput} style={{ height: this.state.inputHeight }} onFocus={() => this.setState({ inputHeight: 100 })} onBlur={() => this.setState({ inputHeight: 40 })} /> <TextInput ref={this.textInput} style={{ height: this.state.inputHeight }} onFocus={() => this.setState({ inputHeight: 100 })} onBlur={() => this.setState({ inputHeight: 40 })} /> </ScrollView> ); } } ``` 在上面的例子中,我们使用 ScrollView 组件包裹了两个 TextInput 组件,并且在 KeyboardDidShow 和 KeyboardDidHide 事件中,调用了 ScrollView 组件的 scrollTo 方法来滚动到当前输入框的位置上。 以上是两种比较常用的解决方法,你可以根据具体的场景选择适合自己的方式来解决问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值