【问题标题】:ScrollView removing TextInput keyboard in React NativeScrollView 在 React Native 中删除 TextInput 键盘
【发布时间】:2020-11-23 09:41:35
【问题描述】:

我正在使用带有自定义清除图标的TextInput 来清除文本。因为我需要在屏幕上保留多个TextInputs,所以我使用了ScrollView:

import React, {Component} from 'react';
    import {
      View,
      Text,
      ScrollView,
      StyleSheet,
      Image,
      TouchableOpacity,
      TextInput,
    } from 'react-native';

    export default class SuccessScreen extends Component {
      constructor(props) {
        super(props);
        this.state = {};
      }

      handleRightClick() {
        console.log('handleRightClick');
      }

      render() {
        return (
          <ScrollView>
            <View style={styles.SectionStyle}>
              <TextInput style={styles.input} />

              <TouchableOpacity
                style={styles.ImageStyle}
                onPress={this.handleRightClick}>
                <Image source={require('../../images/cross_icon.png')} />
              </TouchableOpacity>
            </View>
          </ScrollView>
        );
      }
    }

    const styles = StyleSheet.create({
      
      input: {
        flex: 1,
        borderColor: '#000000',
        borderBottomWidth: 1,
        height: 40,
        fontSize: 15,
        color: '#000000',
      },
      

      SectionStyle: {
        flexDirection: 'row',
        minWidth: '100%',
        height: 40,
      },

      ImageStyle: {
        height: 25,
        width: 25,
        marginLeft: -25,
        alignContent: 'center',
        resizeMode: 'stretch',
        alignItems: 'center',
      },
    });

没有ScrollView,调用handleRightClick,但是当我使用ScrollView时,它只是关闭键盘而不调用handleRightClick。

【问题讨论】:

    标签: react-native scrollview textinput


    【解决方案1】:

    ScrollView 有一个属性 keyboardShouldPersistTaps。您应该将其设置为'handled',以便您的TouchableOpacity 将处理新闻而不是ScrollView。

    <ScrollView keyboardShouldPersistTaps='handled'>
      // ...
    </ScrollView>
    

    【讨论】:

    • keyboardShouldPersistTaps='always' 和 keyboardShouldPersistTaps='handled' 工作正常
    • 但是如果你设置'always',ScrollView 永远不会隐藏键盘。 prop 值取决于所需的行为,我喜欢在我的应用程序中使用'handled'
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-08-11
    • 2021-01-25
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 2020-09-13
    • 2016-01-06
    相关资源
    最近更新 更多