2

我无法强制集中反应原生 TextInput 字段。当页面以autofocus={true}.

但模糊后我需要按文本输入字段,但由于设计原因,这是隐藏的。有没有办法document.getElementById("myText").focus();让我可以在之后调用本机反应onBlur()

干杯。

4

1 回答 1

7

您需要将TextInput字段分配给ref这样的: <TextInput ref={ref => (this.ref = ref)} />

然后,您可以通过在其上运行以编程方式聚焦它focus(),如下所示: this.ref.focus();

这是一个小点心,我在按下按钮时运行焦点/模糊功能: https ://snack.expo.io/@marcelkalveram/auto-focus-input-field-using-ref

为您的导航场景复制它应该很容易。

如果您使用 react-navigation,这是您将感兴趣的文档的一部分:https ://reactnavigation.org/docs/en/navigation-prop.html#addlistener-subscribe-to-updates-到导航生命周期

于 2018-10-11T11:12:19.287 回答