【问题标题】:react-native-keyboard-aware-scroll-view isn't scrolling on Androidreact-native-keyboard-aware-scroll-view 不在 Android 上滚动
【发布时间】:2019-04-02 16:09:06
【问题描述】:

react-native-keyboard-aware-scroll-view 在 Android 上不滚动这是一个问题,因为 当我单击顶部的 textInput 时,它会消失,我无法将其滚动到视图中。

我加了

android:windowSoftInputMode="adjustPan"

到 android 清单,我在文件顶部导入了 react-native-keyboard-aware-scroll-view。这是我的代码。

<View>
        <TouchableOpacity style={this.props.addressDisplayStyle} accessibilityLabel={'addressSelected'} onPress={() => this.setState({showModal: true})}>
          <Text numberOfLines={6} ellipsizeMode ={'tail'} style={[styles.text, styles.secondaryText, styles.selectedText, styles.addressText]}>{this.props.address}</Text>
        </TouchableOpacity>
        <Modal
                animationType="fade"
                transparent={true}
                visible={this.state.showModal}
            onRequestClose={() => this.closeModal()}
              >
          <KeyboardAwareScrollView
            resetScrollToCoords={{ x: 0, y: 0 }}
            contentContainerStyle={[styles.fadedBackground, { justifyContent: 'center', flexGrow: 1}]}
            scrollEnabled={true}
            enableAutomaticScroll={(Platform.OS === 'ios')}
            enableOnAndroid={true}
          >
              <View style={styles.modalContainer}>
                <Text style={[styles.text, styles.titleText]}>Enter Address</Text>
            <TextInput
              maxLength={300}
              multiline = {false}
              placeholder = {'123 Street'}
              style = {[styles.text, styles.inputText, styles.inputTextCustom]}
              onChangeText={(changedText) => this.setState({street: changedText})}
              value={this.state.street}
            />
            <TextInput
              maxLength={300}
              multiline = {false}
              placeholder = {'Apt #'}
              style = {[styles.text, styles.inputText, styles.inputTextCustom]}
              onChangeText={(changedText) => this.setState({street2: changedText})}
              value={this.state.street2}
            />
            <TextInput
              maxLength={300}
              multiline = {false}
              placeholder = {'city'}
              style = {[styles.text, styles.inputText, styles.inputTextCustom]}
              onChangeText={(changedText) => this.setState({city: changedText})}
              value={this.state.city}
            />
            <TextInput
              maxLength={2}
              multiline = {false}
              placeholder = {'State'}
              style = {[styles.text, styles.inputText, styles.inputTextCustom]}
              onChangeText={(changedText) => this.setState({state: changedText})}
              value={this.state.state}
            />
            <TextInput
              maxLength={10}
              keyboardType={'numeric'}
              multiline = {false}
              placeholder = {'Zipcode'}
              style = {[styles.text, styles.inputText, styles.inputTextCustom]}
              onChangeText={(changedText) => this.setState({zipcode: changedText})}
              value={this.state.zipcode}
            />
            {this.state.loading?
              <Loading />
              :
              null
            }
              </View>
        </KeyboardAwareScrollView>
            </Modal>
      </View>

我尝试在 KeyboardAwareScrollView 之前和之后添加滚动视图,并有一个视图来扭曲它,但我似乎无法让它在 android 上工作。

【问题讨论】:

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


    【解决方案1】:

    这就是我在键盘出现和滚动问题时处理文本输入隐藏的方式。我确实使用了这个库,但没有得到想要的结果。我查看了源代码并提出了自己的使用原生组件的解决方案。

    这是我的渲染方法:请注意,我创建了一些包装组件,例如 Button 和 CustomizedTextInput,但 ScrollView 的属性应该可以满足您的需求。

      <ScrollView keyboardShouldPersistTaps="handled"
          showsVerticalScrollIndicator={false}>
    
            {this.renderImage(styles)}
            <View style={styles.container}>
            <View style={styles.buttons}>
                <TouchableOpacity
                  style={[styles.button,social,{justifyContent:'center'}]}>
                  <Icon name={'logo-facebook'} size={25} style={[awesome,hero,accentColor,center,{alignSelf:'center'}]}/>
                </TouchableOpacity>
                <TouchableOpacity
                  style={[styles.button,social,{justifyContent:'center'}]}
                  >
                  <Icon name={'logo-twitter'} size={25} style={[awesome,hero,accentColor,center,{alignSelf:'center'}]}/>
                </TouchableOpacity>
                <TouchableOpacity
                  style={[styles.button,social,{justifyContent:'center'}]} >
                  <Icon name={'logo-google'} size={25} style={[awesome,hero,accentColor,center,{alignSelf:'center'}]}/>
                </TouchableOpacity>
              </View>
              <CustomizedTextInput
                placeholder='Username'
                autoCapitalize="none"
                autoCorrect={false}
                backgroundColor={Theme[this.props.theme].colors.control.background}
                borderColor={Theme[this.props.theme].colors.border.base}
                borderRadius={24}
                placeholderTextColor={baseColor.color}
                value={this.state.username}
                onChangeText={val => this.onChangeText('username', val)}
              />
              <CustomizedTextInput
                placeholder='Password'
                autoCapitalize="none"
                autoCorrect={false}
                backgroundColor={Theme[this.props.theme].colors.control.background}
                borderColor={Theme[this.props.theme].colors.border.base}
                borderRadius={24}
                placeholderTextColor={baseColor.color}
                value={this.state.password}
                onChangeText={val => this.onChangeText('password', val)}
              />
                  <Button
                      text='LOGIN'
                      borderRadius={24}
                      color={Theme[this.props.theme].colors.gradients.base[0]}
                      style={[{width: 300},{height:50},baseColor,styles.save]}
                      textStyle={[inverseColor]}
                      onPress={this.signIn}
                    />
    
                    <View style={styles.footer}>
                    <View style={styles.textRow}>
                      <Text style={[primary3, baseColor]}>Don’t have an account? </Text>
                      <TouchableOpacity
                      onPress={()=>this.props.navigation.navigate('auth.signup')}
                      style={[clear]} >
                      <Text style={[header6, baseColor]}>Sign up now</Text>
                    </TouchableOpacity>
                    </View>
                  </View>
    
    
              </View>
              </ScrollView>
    

    【讨论】:

    • 感谢 Ahsan,我没有意识到 scrollview 有 keyboardShouldPersistTaps,我仍然没有理想的功能,但这确实允许更好的用户体验,所以我删除了另一个库。
    • 对您来说,原生组件总是比第三方库更好......但是,获得理想功能也总是不同。我的建议是在大多数情况下使用第三方库之前查看源代码,解决方案只是一行。
    猜你喜欢
    • 2017-03-16
    • 2020-09-17
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多