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

时间:2018-04-14 22:08:37

标签: javascript reactjs react-native react-native-ios react-native-flatlist

用户需要在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>
    );
  }
}

当用户在<FlatList>

中搜索时,该组件的目的是在<TextInput>中提供自动提示

1 个答案:

答案 0 :(得分:3)

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