强制焦点 ReactNative TextInput

Rub*_*ers 2 react-native react-native-android react-native-ios react-native-textinput

我在强制聚焦本机 TextInput 字段时遇到了麻烦。当页面加载autofocus={true}.

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

干杯。

Mar*_*ram 5

您需要将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-导航生命周期

  • 嗯,我收到一个 ref is not defined 错误,我真的从你的零食中复制了代码? (2认同)