【发布时间】:2020-08-22 14:00:26
【问题描述】:
我想在用户点击按钮时显示模式,而不关闭键盘。不幸的是,模式一出现,键盘就会消失。
最小复制案例:
import * as React from "react";
import { Button, Modal, Text, TextInput, View } from "react-native";
function TestComp() {
const [showingModal, setshowingModal] = React.useState(false);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center", marginTop: 22 }}>
<Modal visible={showingModal} transparent onRequestClose={() => setshowingModal(false)}>
<View style={{ flex: 1, marginTop: 22 }}>
<Button title={"hide modal"} onPress={() => setshowingModal(false)} />
</View>
</Modal>
<TextInput placeholder="Focus to show keyboard" />
<Button title={"Show modal"} onPress={() => setshowingModal(true)} />
</View>
);
}
仅供参考,我正在使用博览会。
如何强制键盘保持不变?
【问题讨论】:
-
当您点击
TextInput时,键盘会打开,从而获得焦点;当您点击按钮以显示模式时,TextInput失去焦点,因此键盘关闭。打开 modal 时,你想在哪里设置键盘焦点? -
@ChristosLytras,按下按钮不会移除键盘上的焦点(例如,我可以在
onPress中放置console.log)。消除焦点的是专门显示的模态。
标签: react-native expo react-native-modal