[英]In React Native, how can I avoid having a user click a submit button twice, when they are writing in a TextInput?
I have a very simple form for writing comments, with a <TextInput>
and an <IconButton>
:我有一个非常简单的用于编写评论的表单,其中包含一个
<TextInput>
和一个<IconButton>
:
Screenshot:截屏:
Code:代码:
<View style={styles.container}>
<TextInput
mode={'outlined'}
onChangeText={(val) => setCommentText(val)}
value={commentText}
label={'Write a comment...'}
/>
<IconButton
icon="send"
onPress={addComment}
/>
</View>
The problem is that users have to click the Send button twice.问题是用户必须单击发送按钮两次。 The first click just blurs the input and dismisses the keyboard, but the button doesn't register a click.
第一次点击只会模糊输入并关闭键盘,但按钮不会记录点击。 Then when the TextInput is blurred they can actually click the Submit button.
然后,当 TextInput 模糊时,他们实际上可以单击提交按钮。
How can I make it possible to click the Send button only once, when the TextInput
has focus?当
TextInput
具有焦点时,如何才能仅单击一次发送按钮?
Changing onPress
to onTouchStart
in the <Button>
solved it.在
<Button>
中将onPress
更改为onTouchStart
解决了它。 No need for tapping the button twice anymore.无需再点击按钮两次。
Actually this behaviour is normal.其实这种行为是正常的。 if the keyboard is open and you want to interact with screen, first click will dismiss the keyboard and second one will work.
如果键盘打开并且您想与屏幕交互,则第一次单击将关闭键盘,第二次单击将起作用。 so as the best practice you can have a button within your keyboard which clicking on that will trigger the call request you want:
因此,作为最佳实践,您可以在键盘上设置一个按钮,单击该按钮将触发您想要的呼叫请求:
<TextInput
mode={'outlined'}
onChangeText={(val) => setCommentText(val)}
value={commentText}
returnKeyType="send"
onSubmitEditing={() => {
// call you api or whatever function here;
}}
label={'Write a comment...'}
blurOnSubmit={false}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.