美文网首页
React Native 键盘的弹出与隐藏

React Native 键盘的弹出与隐藏

作者: 小雨TT | 来源:发表于2017-08-09 16:01 被阅读0次

    1. 添加监听

    componentWillMount() {
        this.keyboardWillShowSub = Keyboard.addListener('keyboardDidShow', () => this.keyboardWillShow());
        this.keyboardWillHideSub = Keyboard.addListener('keyboardDidHide', () => this.keyboardWillHide());
    }
    
    componentWillUnmount() {
        this.keyboardWillShowSub.remove();
        this.keyboardWillHideSub.remove();
    }
    
    keyboardWillHide() {
       // 做想做的事,比如解决键盘弹出遮挡input框的问题
    
    };
    
    keyboardWillShow() {
       //
    
    };
    

    2. TextInput 控件外层用 KeyboardAvoidingView 包裹

    render() {
        return (<KeyboardAvoidingView >
            <TextInput
                style={[styles.input, {marginTop: 40}]}
                placeholder="请输入用户名"
                underlineColorAndroid='transparent' //设置下划线背景色透明 达到去掉下划线的效果
                onChangeText={(text) => this.setState({userName: text})}
            />
            <TextInput
                style={[styles.input]}
                secureTextEntry={true}  //密文
                placeholder="请输入密码"
                underlineColorAndroid='transparent' //设置下划线背景色透明 达到去掉下划线的效果
                onChangeText={(text) => this.setState({password: text})}
            />
            <TouchableHighlight
                style={styles.loginButton}
                onPress={() => {
                  
                }}>
                <Text style={[styles.loginButtonText]}
                >登录</Text>
            </TouchableHighlight>
        </KeyboardAvoidingView>);
    }

    相关文章

      网友评论

          本文标题:React Native 键盘的弹出与隐藏

          本文链接:https://www.haomeiwen.com/subject/wgkqrxtx.html