React Native必须双击onPress才能工作

问题描述 投票:20回答:2

我正在使用React Native和Native Base库。当键盘打开时,我需要一个onPress事件来触发Native Base的ListItem(相当于TouchableOpacity)。

我现在必须单击一次关闭键盘,然后我可以按ListItem。

下面的内容标记等同于ScrollableView:

<Content keyboardShouldPersistTaps='always' keyboardDismissMode='on-drag'>
  <List>
    <ListItem style={styles.inspectionsItemDivider} itemDivider>
      <TextInput
        autoFocus={true}
        ref={(input) => { this.titleSearch = input }}
        placeholder='Start typing...'
        multiline={true}
        onChangeText={this.setSearchText.bind(this)}
        value={this.getSearchValue()}/>
    </ListItem>
    <View style={styles.searchContainer}>
      <Text style={styles.recentText}>Recommended Descriptions</Text>
      <List dataArray={this.state.searchedDescriptions} 
        renderRow={(description) =>
        <ListItem button onPress={() => this.setInformationDescription(description)}>
          <Text>{description}</Text>
        </ListItem>
      </List>
    </View>
  </List>
</Content>
react-native native-base
2个回答
32
投票

我其实只是想出来了。除了Content标签之外,我还将keyboardShouldPersistTaps ='always'道具添加到我的列表中:

<Content keyboardShouldPersistTaps='always' keyboardDismissMode='on-drag'>
  <List>
    <ListItem style={styles.inspectionsItemDivider} itemDivider>
      <TextInput
        autoFocus={true}
        ref={(input) => { this.titleSearch = input }}
        placeholder='Start typing...'
        multiline={true}
        onChangeText={this.setSearchText.bind(this)}
        value={this.getSearchValue()}/>
    </ListItem>
    <View style={styles.searchContainer}>
      <Text style={styles.recentText}>Recommended Descriptions</Text>
      <List keyboardShouldPersistTaps='always' dataArray={this.state.searchedDescriptions} 
        renderRow={(description) =>
        <ListItem button onPress={() => this.setInformationDescription(description)}>
          <Text>{description}</Text>
        </ListItem>
      </List>
    </View>
  </List>
</Content>

1
投票

对于新的googlers,在我的情况下,我有一个带有onPress属性的flatlist和一个searchBar。我想在键盘启动时按下该行,我只能通过双击来完成。最后,通过使用平板列表的“keyboardShouldPersistTaps”来解决问题:

Hide_Soft_Keyboard=()=>{
      Keyboard.dismiss();
    }

....

                <List>
                      <FlatList
                        keyboardShouldPersistTaps = "always"
                        ...
                        renderItem={({item}) => (
                        <ListItem
                          ...
                          ...
                          onPress={() =>{this.Hide_Soft_Keyboard(); this.props.navigation.navigate('Screen2')}}                                                      
                        /> ) }
                        />
                  </List>
© www.soinside.com 2019 - 2024. All rights reserved.