React Native 键盘的弹出与隐藏

2017-08-09  本文已影响0人  小雨TT

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>);
}
上一篇 下一篇

猜你喜欢

热点阅读