简体   繁体   English

在 React Native 中,当用户在 TextInput 中写入时,如何避免用户点击提交按钮两次?

[英]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.

相关问题 当文本输入和提交按钮都在 React Native 中的不同 js 文件中时,如何在单击提交时进行空检查并导航到新屏幕? - How to null check and navigate to new screen on click submit when textinput and submit button both are in different js files in react native? 选择时 React Native TextInput 缩小,我该如何停止它? - React Native TextInput shrinks when selected, how can I stop it? 如何在按钮单击时从 React Native TextInput 中移除焦点? - How to remove focus from a React Native TextInput on button click? 单击按钮时清除TextInput值-React Native - clear TextInput value on button click - React Native 反应原生在按钮单击时添加文本输入? - react native adding textinput on button click? 单击按钮,React Native将文本添加到TextInput - React Native add text to TextInput on button click TextInput聚焦时如何单击行? (反应天然) - How to click on row when TextInput is focused? (react-native) 我知道react-native的TextInput有一个onsubmit回调函数,但我如何实际提交该提交按钮? - I know react-native's TextInput has a onsubmit callback function, but how do i actually make that submit button? React Native无法单击TouchableOpacity或TextInput - React Native can't click TouchableOpacity or TextInput 如何在 React Native 中对 TextInput 应用单击事件 - How to apply click event on TextInput in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM