键盘打开 React Native 时出现双击按钮问题

use*_*028 14 keyboard-events react-native

我知道已经有很多关于这个主题的疑问,我已经尝试了每一步,但仍然无法解决问题。

这是代码:

    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
    );

  }
Run Code Online (Sandbox Code Playgroud)

**我什至按照@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>
Run Code Online (Sandbox Code Playgroud)

我已经关注了这篇文章

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

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

谢谢。

Has*_*Eqx 13

keyboardShouldPersistTaps='handled'您可以在 ScrollView 或 Scrollables(如 FlatList、SectionList 等)中使用,并嵌入 TouchableWithoutFeedBack 来处理外部点击时关闭的情况。

<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
 <ScrollView keyboardShouldPersistTaps='handled'>
   // Rest of the content.
 </ScrollView/>
</TouchableWithoutFeedback>
Run Code Online (Sandbox Code Playgroud)

对于 FlatList 和 SectionList,您必须KeyBoard.dismiss单独处理。


Nir*_*inh 10

您需要在KeyboardShouldPersistTaps中始终添加给定值,以允许用户在不关闭键盘的情况下点击。

keyboardShouldPersistTaps='always'
Run Code Online (Sandbox Code Playgroud)

例如:

<ScrollView keyboardShouldPersistTaps='always'>
 // Put your component
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

注意:请将您的可点击组件放入 ScrollView 中。否则就行不通。


归档时间:

查看次数:

8725 次

最近记录:

3 年,1 月 前