当虚拟键盘出现在react native中时,如何自动向上滚动屏幕(在android上)

时间:2020-03-06 14:18:22

标签: android react-native scrollview textinput android-virtual-keyboard

虚拟键盘覆盖了文本输入,我看不到我在输入什么。如何避免这种情况? (我尝试使用KeyboardAwareScrollView,但它仍然涵盖了文本输入)。 我遇到的另一个问题是关于我的styles.container属性的错误-> justifyContent和alignItems-它说将它们放在ScrollView属性中-我不确定该怎么做-我是React native的新手。

  render() {
    return (
      <KeyboardAwareScrollView>
      <View style={styles.container} >
         <TextInput
          style={styles.input}
          underlineColorAndroid="transparent"
          placeholder="username"
          placeholderTextColor="#00bcd4"
          autoCapitalize="none"
          secureTextEntry={true}
          onChangeText={username => this.setState({ username })}  

          />
          <View style={styles.btnContainer}>     
         <TouchableOpacity style={styles.userBtn}  onPress={() => this.Login(this.state.email, this.state.password)}>
                   <Text style={styles.btnTxt}>Login</Text>
         </TouchableOpacity>

        </View>
      </View>
      </KeyboardAwareScrollView>

也可以使用KeyboardAvoidingView进行相同操作:

  render() {
    return (
      // <View style={styles.container} >
         <KeyboardAvoidingView behavior="padding" style={styles.container}>
         <Text style={styles.heading} >Welcome to SelfCare !</Text>
          <Image
          style={{width: 200, height: 200, marginBottom: 40}}
          source={require('./src/images/icon.png')}
          />
         <TextInput
          style={styles.input}
          underlineColorAndroid="transparent"
          placeholder="Email"
          placeholderTextColor="#00bcd4"
          autoCapitalize="none"
          onChangeText={email => this.setState({ email })}  
             />
         <TextInput
          style={styles.input}
          underlineColorAndroid="transparent"
          placeholder="Password"
          placeholderTextColor="#00bcd4"
          autoCapitalize="none"
          secureTextEntry={true}
          onChangeText={password => this.setState({ password })}  

          />
          <View style={styles.btnContainer}>     
         <TouchableOpacity style={styles.userBtn}  onPress={() => this.Login(this.state.email, this.state.password)}>
                   <Text style={styles.btnTxt}>Login</Text>
         </TouchableOpacity>
         <TouchableOpacity style={styles.userBtn}  onPress={() => this.SignUp(this.state.email, this.state.password)}>
                   <Text style={styles.btnTxt}>Sign Up</Text>
         </TouchableOpacity>         
        </View>
        </KeyboardAvoidingView>
      // </View>
    );
  }
}

keyboard covers textinput

1 个答案:

答案 0 :(得分:2)


如果希望屏幕升起,可以使用
<KeyboardAvoidingView> 代替 <KeyboardAwareScrollView>
,除非您希望用户在打开键盘的同时能够滚动屏幕,否则它更易于实现并完成相同的工作。

behavior='padding' <<此道具用于屏幕操作。它还将[height,position]作为值。

<View style={styles.container}>
   <KeyboardAvoidingView behavior="padding">

         ... Your UI ...

   </KeyboardAvoidingView>
</View>

或者简而言之,您可以将<View>替换为<KeyboardAvoidingView>

<KeyboardAvoidingView behavior="padding" style={styles.container}>

         ... Your UI ...

</KeyboardAvoidingView>