我在我的文本输入中添加了一个 onKeyPress 事件监听器,这样当用户在电话键盘上单击完成、开始或“输入”时,它将调用我的 searchProducts 函数。我希望有一个 onSubmit 类型的事件监听器选项,但找不到类似的东西,所以现在我只能简单地检查每个按下的键。当按下字符键(例如“j”或“x”)时,它会触发 onKeyPress 事件并调用我的 searchProducts 函数。但是,当我用鼠标单击完成或按下键盘上的回车键时,没有任何反应。我如何让它触发 onKeyPress 事件监听器??
searchProducts = (e) => {
if (e.nativeEvent.key == "Enter"){
this.props.searchFunc(this.state.term);
}
}
<TextInput
ref='searchBar'
style={styles.searchInput}
placeholder={placeholder}
onChangeText={this.searchSuggestions}
onKeyPress={this.searchProducts.bind(this)}
underlineColorAndroid="transparent"
/>
最佳答案
您可以使用 onSubmitEditing这是:
Callback that is called when the text input's submit button is pressed. Invalid if multiline={true} is specified.
它将在按下键盘上的“完成”按钮后被调用。
关于android - 为什么 "Enter"键在 React Native 中不触发 onKeyPress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50584382/