【发布时间】:2019-10-26 07:20:11
【问题描述】:
我正在使用 react native 开发一个应用程序,我们有设计师制作的屏幕布局。但我无法正确实现预期的行为。基本上它是一个带有一些文本输入和一个按钮的屏幕,当键盘出现时我需要正确调整东西。以下是预期的屏幕:
所以当键盘出现时,按钮必须向上很多,两个文本输入都会上升一点,顶部的文本保持不变。没有键盘的屏幕看起来很完美,但现在当键盘出现时它什么也不做。我尝试了很多东西,都没有真正奏效。现在是渲染方法:
render() {
const textInputBorderColor = this.state.hasError ? Colors.error : Colors.background;
const textInputCpfMarginTop = this.state.hasError ? 24 : 48;
return (
<View style = {styles.container}>
<KeyboardAvoidingView behavior='padding'>
<Text style = {styles.headerText}>Vamos começar!</Text>
<TextInput
value = {this.props.user.name}
onChangeText = {text => this.props.user.name = text}
placeholder = 'Insira aqui seu nome completo'
style = {[styles.textInputName, {borderColor: textInputBorderColor}]}
/>
<ErrorText show = {this.state.hasError} value = {this.state.errorMsg}/>
<TextInputMask
value = {this.props.user.cpf}
onChangeText = {text => this.props.user.cpf = text}
placeholder = 'e aqui seu CPF'
keyboardType = 'numeric'
type = 'cpf'
style = {[styles.textInputCpf, {borderColor: Colors.background, marginTop: textInputCpfMarginTop}]}
/>
<View style = {{marginTop: 202}}>
<DefaultButton
onPress = {this.onButtonPress}
btnLabel = 'Continuar'
disabled = {(this.props.user.name == '' || this.props.user.cpf.length != 14)}
/>
</View>
</KeyboardAvoidingView>
</View>
);
}
样式:
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFFFFF',
alignItems: 'center',
},
textInputName: {
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 16,
paddingBottom: ScreenDimensions.width * 0.01,
borderBottomWidth: 1,
marginTop: 96,
width: 321
},
textInputCpf: {
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 16,
paddingBottom: ScreenDimensions.width * 0.01,
borderBottomWidth: 1,
width: 321
},
headerText: {
marginTop: 66,
textAlign: 'center',
fontFamily: 'Roboto-Light',
fontSize: 20,
color: '#000'
}
})
关于这个组件(keyboardAvoidingView)的文档一文不值……
非常感谢您的帮助!
【问题讨论】:
-
您是否厌倦了将按钮放在键盘外以避免查看?
-
是的,还是什么都没有:/
-
您是否也尝试将
flex:1添加到KeyboardAvoidingView中?在我看来,你甚至不需要最外面的<View>,只需将容器样式传递给KeyboardAvoidingView,它应该是一样的 -
是的,最外面的视图是不需要的,但仍然没有,我只是添加了一个滚动视图来帮助用户。
标签: react-native keyboard jsx