keyBoard打开React本机时出现双击按钮问题

时间:2018-12-26 06:50:30

标签: react-native keyboard-events

我知道有关此主题的查询已经很多,我已经尝试了所有步骤,但仍然无法解决问题。

这是代码:

    render() {
    const {sContainer, sSearchBar} = styles;

    if (this.props.InviteState.objectForDeleteList){
      this.updateList(this.props.InviteState.objectForDeleteList);
    }
    return (
      <View style={styles.mainContainer}>
        <CustomNavBar
          onBackPress={() => this.props.navigation.goBack()}
        />
        <View
          style={sContainer}
        >
          <ScrollView keyboardShouldPersistTaps="always">
            <TextInput
              underlineColorAndroid={'transparent'}
              placeholder={'Search'}
              placeholderTextColor={'white'}
              selectionColor={Color.colorPrimaryDark}
              style={sSearchBar}
              onChangeText={(searchTerm) => this.setState({searchTerm})}
            />
          </ScrollView>
          {this.renderInviteUserList()}
        </View>
      </View>
    );
  }

renderInviteUserList() {
    if (this.props.InviteState.inviteUsers.length > 0) {
      return (
        <SearchableFlatlist
          searchProperty={'fullName'}
          searchTerm={this.state.searchTerm}
          data={this.props.InviteState.inviteUsers}
          containerStyle={styles.listStyle}
          renderItem={({item}) => this.renderItem(item)}
          keyExtractor={(item) => item.id}
        />
      );
    }
    return (
      <View style={styles.emptyListContainer}>
        <Text style={styles.noUserFoundText}>
          {this.props.InviteState.noInviteUserFound}
        </Text>
      </View>
    );
  }


renderItem(item) {
    return (
      this.state.userData && this.state.userData.id !== item.id
        ?
        <TouchableOpacity
          style={styles.itemContainer}
          onPress={() => this.onSelectUser(item)}>
          <View style={styles.itemSubContainer}>
            <Avatar
              medium
              rounded
              source={
                item.imageUrl === ''
                  ? require('../../assets/user_image.png')
                  : {uri: item.imageUrl}
              }
              onPress={() => console.log('Works!')}
              activeOpacity={0.7}
            />
            <View style={styles.userNameContainer}>
              <Text style={styles.userNameText} numberOfLines={1}>
                {item.fullName}
              </Text>
            </View>
            <CustomButton
              style={{
                flexWrap: 'wrap',
                alignSelf: 'flex-end',
                marginTop: 10,
                marginBottom: 10,
                width: 90,
              }}
              showIcon={false}
              btnText={'Add'}
              onPress={() => this.onClickSendInvitation(item)}
            />
          </View>
        </TouchableOpacity> : null
    );

  }

**我什至尝试使用@Nirmalsinh **建议的波纹管代码:

<ScrollView keyboardShouldPersistTaps="always" style={sContainer}>
        <CustomNavBar
          onBackPress={() => this.props.navigation.goBack()}
        />
        <TextInput underlineColorAndroid={'transparent'}
          placeholder={'Search'}
          placeholderTextColor={'white'}
          selectionColor={Color.colorPrimaryDark}
          style={sSearchBar}
          onChangeText={(searchTerm) => this.setState({searchTerm})} />
        {this.renderInviteUserList()}
      </ScrollView>

我已经关注了这篇文章

  

https://medium.com/react-native-training/todays-react-native-tip-keyboard-issues-in-scrollview-8cfbeb92995b

我也尝试过使用 keyboardShouldPersistTaps = handled ,但仍然必须在“自定义按钮”上点按两次以执行操作。谁能告诉我代码中我在做什么错?

谢谢。

2 个答案:

答案 0 :(得分:0)

您需要在 keyboardShouldPersistTaps 中添加始终值,以允许用户点击而不关闭键盘。

keyboardShouldPersistTaps='always'

例如:

<ScrollView keyboardShouldPersistTaps='always'>
 // Put your component
</ScrollView>

注意:请将可点击的组件放入ScrollView中。否则它将无法正常工作。

答案 1 :(得分:0)

您可以在ScrollView或FlatList SectionList等Scrollable中使用keyboardShouldPersistTaps='handled',并嵌入TouchableWithoutFeedBack来处理因外部点击而被驳回的情况。

<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
 <ScrollView keyboardShouldPersistTaps='handled'>
   // Rest of the content.
 </ScrollView/>
</TouchableWithoutFeedback>

对于FlatList和SectionList,您将不得不分别处理KeyBoard.dismiss