在 React Native 中,是否可以在键盘打开时调用 onPress

In React Native, is it possible to call onPress while keyboard open

用户需要在 FlatList 项上单击两次,因为 autoFocus={true} 对应 <TextInput。第一次点击键盘隐藏,然后点击调用 onPress={this.GetItem.bind(this, item)}。是否有任何选项可以在第一次点击时调用 GetItem() 而不是点击两次。

演示:https://snack.expo.io/ByJ_yWehM

export default class App extends Component {
  GetItem (item) {
    console.log(item);
    Alert.alert(item);
  }
  render() {
    return (
      <View style={styles.container}>
        <TextInput
          autoFocus={true}
          style={styles.paragraph}
          keyboardType='web-search'
        >
          Change code in the editor and watch it change on your phone!
          Save to get a shareable url.
        </TextInput>
        <Card title="Local Modules">
          <View>
            <TextInput
              style={styles.searchField}
              placeholder="Type here to translate!"
              onChangeText={(text) => this.setState({text})}
            />

            <FlatList
                data={["akin","alike","close","comparable","like","similar","uniform","Allied","Analogous","Co-ordinate","Commensurate","akin","alike","close","comparable","like","similar","uniform","Allied","Analogous","Co-ordinate","Commensurate"]}
                renderItem={({item}) => (
                  <Text
                    style={styles.listField}
                    onPress={this.GetItem.bind(this, item)}
                    >{item}</Text>
                )}
              />
          </View>
        </Card>
      </View>
    );
  }
}

该组件的目的是当用户在 <TextInput>

中搜索时在 <FlatList> 中提供自动建议

keyboardShouldPersistTaps='handled' 添加到您的 FlatList 将防止键盘被关闭 onPress

<FlatList
    keyboardShouldPersistTaps='handled'
    data={["akin","alike","close","comparable","like","similar","uniform","Allied","Analogous","Co-ordinate","Commensurate","akin","alike","close","comparable","like","similar","uniform","Allied","Analogous","Co-ordinate","Commensurate"]}
    renderItem={({item}) => (
      <Text
        onPress={this.GetItem.bind(this, item)}
        >{item}</Text>
    )}
/>

always 也可以用作 keyboardShouldPersistTaps 值。

Official doc for keyboardShouldPersistTaps here