【问题标题】:React Native two time taps required for onPress event inside scrollview滚动视图内的 onPress 事件需要 React Native 两次点击
【发布时间】:2019-10-25 11:27:07
【问题描述】:

我在 ScrollView 和 KeyboardAvoidingView 中使用 TextInput,但它需要在键盘打开时点击两次才能提交文本。
对于解决方案,我添加了 keyboardShouldPersistTaps="always" 但它不起作用。

render() {
    return (
        <View style={{flex: 1}}>
            <KeyboardAvoidingView style={{flex:1}}>
            <ScrollView keyboardShouldPersistTaps="always" 
                contentContainerStyle={{
                    paddingHorizontal: 10,
                    flexGrow : 1,
                    justifyContent : 'center',
                    alignItems:'center'}}>
                <View style={{backgroundColor:'green', width:'100%', borderRadius:8, overflow:'hidden'}}>
                    <TextInput style={{margin:10}}/>
                    <TouchableOpacity onPress={() => {alert('alert')}}>
                        <Text>Submit</Text>
                    </TouchableOpacity>
                </View>
            </ScrollView>
            </KeyboardAvoidingView>
        </View>
    )
}

如何在单击时触发新闻事件?

【问题讨论】:

标签: reactjs react-native button tap onpress


【解决方案1】:

使用"handled" 代替"always"keyboardShouldPersistTaps="handled"

【讨论】:

  • 它有效(keyboardShouldPersistTaps="handled" 与您第一次写的一样,带有“d”)
【解决方案2】:

你必须像这样编写 TouchableOpacity 组件的 onPress 方法

 <TouchableOpacity
onPress={() => alert('Clicked)}>
      <Text>Submit</Text>
 </TouchableOpacity>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多