【问题标题】:Double Tap Button issue when keyBoard opens React native键盘打开 React native 时双击按钮问题
【发布时间】:2018-12-26 06:50:30
【问题描述】:

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

代码如下:

    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,但我仍然必须在自定义按钮上点按两次才能执行操作。谁能告诉我我在代码中做错了什么?

谢谢。

【问题讨论】:

    标签: react-native keyboard-events


    【解决方案1】:

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

    keyboardShouldPersistTaps='always'
    

    例如:

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

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

    【讨论】:

    • 嗨@Nirmalsinh,已经在那里: this.setState({searchTerm})} /> {this.renderInviteUserList()}
    • 你把按钮代码放在哪里了?它在滚动视图内吗?
    • 不,它不在滚动视图内,我只是将搜索框放在滚动视图内
    • 这是个问题。我们需要将所有组件保留在滚动视图中以获取事件。
    • this.setState({searchTerm})} /> {this.renderInviteUserList()}
    【解决方案2】:

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

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

    对于 FlatList 和 SectionList,您必须分别处理 KeyBoard.dismiss

    【讨论】:

    • 赞成...检查文档以了解“始终”和“已处理”之间的区别。对我来说,“处理”的效果要好得多。 “当[孩子喜欢按下按钮] 处理点击时,键盘不会自动关闭。”
    猜你喜欢
    • 2015-09-13
    • 2020-06-19
    • 1970-01-01
    • 2017-11-25
    • 2016-11-11
    • 2020-04-24
    • 1970-01-01
    • 2016-09-22
    • 2020-08-25
    相关资源
    最近更新 更多