【问题标题】:Keyboard isn't shown upon TextInput autoFocus [react native]TextInput autoFocus [react native] 上未显示键盘
【发布时间】:2019-09-15 19:58:34
【问题描述】:

我的应用中有这个回复按钮,当用户按下它时,它会将TextInput autoFocus 更改为true。我将 autoFocus 值设置为 false 作为默认值,并将其保持在一个状态。我看到状态将变为 true 但它没有打开键盘。

这是我的文本输入:

<TextInput
    autoFocus={this.state.textInputFocus}
    selectTextOnFocus={true}
    ref={ref => this.textInputRef = ref}
    multiline = {true}
    placeholder="Write a comment ..."
    onChangeText={(postComment) => this.setState({postComment})}
    value={this.state.postComment} />

这是按下回复按钮时更改状态的功能:

_openReplyBox(comment_id, commenter){
    this.setState({ postComment: commenter, textInputFocus: true })
}

【问题讨论】:

标签: react-native keyboard textinput autofocus


【解决方案1】:

根据文档:

autoFocus:如果为真,则将输入聚焦在 componentDidMount 上。默认值为假

您可以使用 refs 来实现相同的功能。

 <TextInput
        ref={"textRef"}
        ...
      />

在打开回复框:

_openReplyBox(comment_id, commenter){
    this.refs.textRef.focus();
    this.setState({ postComment: commenter})
}

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2020-04-25
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2020-05-28
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多