如何长按TextInput触发一个事件,而长按触发编辑TextInput值?

时间:2019-08-22 13:01:58

标签: react-native

我正在创建一个列表,并且列表项的标题实际上是一个TextInput,因此可以更轻松地编辑标题。另外,我想在长按列表项时显示一个选项列表,我还希望这包括在TextInput上长按。

许多人说,我应该在TextInput中添加pointerEvents="none",但这也将阻止编辑该值,而这不是我想要的。

我尝试了下面的代码,但是长按TextInput本身不起作用,这是我要解决的问题。

<ListItem
  onLongPress = {onLongPressed}
>
  <Text>{Num}</Text>
  <TextInput
    placeholder="Enter title..."
    defaultValue="Title"
  />
</ListItem>

1 个答案:

答案 0 :(得分:0)

您可以使用focus

 constructor(props) {
    super(props);
    this.textInput = React.createRef();
    this.state={
        edit: false
    }
  }
...
  onLongPressed = () => {
     this.setState({ edit: true }, () => this.textInput.current.focus());

  }

  <ListItem
      onLongPress = {this.onLongPressed}
      title={
        <View style={styles.titleView}>
          <Text>{Num}</Text>
         <TextInput
           ref={this.textInput}
           placeholder="Enter title..."
           defaultValue="Title"
           editable={this.state.edit}
          />
        </View>
      }
    />