<TextInput> 如何在活动结束后重新聚焦?



当屏幕加载时,我使用以下代码来关注a:

useEffect(() => {
InteractionManager.runAfterInteractions(() => {
if (refText.current) {
refText.current.focus();
}
});
}, []);

按预期工作。然而,在提交事件上,需求再次集中。

function myEvent() {
if (refText.current) {
refText.current.focus();
}
}

这不会再次关注文本。什么好主意吗?

实际上,我不知道为什么你需要在你的提交处理程序中使用if语句来将焦点设置在TextInput上,但无论如何,我认为你在Button声明上有一个事件处理程序。例句:

function App() {
const textRef = useRef();
const handleSubmit = () => {
if (textRef.current) textRef.current.focus();
};
return (
<View>
<TextInput placeholder="Input" ref={textRef}/>
<Button title="Submit" onPress={handleSubmit} />
</View>
);
}
});

就像你在这个沙盒中看到的那样,它工作得很好。

最新更新