javascript - TextInput 在 React Native 中的键盘下消失
问题描述
我正在制作一个反应本机应用程序,由于某种原因,当我点击文本输入以键入一些内容时,它会在键盘下消失。我希望它像 Instagram Messenger 和其他 Messenger 应用程序那样位于键盘顶部。我尝试使用keyboardAvoidView 并将行为设置为填充和高度,但都没有奏效。
import {
View,
Text,
StyleSheet,
SafeAreaView,
TextInput,
TouchableOpacity,
} from "react-native";
import CommunityPost from "../components/CommunityPost";
import { Context as CommunityContext } from "../context/CommunityContext";
const Key = ({ navigation }) => {
const { createCommunityPost, errorMessage } = useContext(CommunityContext);
const [body, setBody] = useState("");
return (
<View style={styles.container}>
<SafeAreaView />
<CommunityPost />
<View style={styles.inputContainer}>
<TextInput
style={styles.input}
multiline={true}
placeholder="Type..."
placeholderTextColor="#CACACA"
value={body}
onChangeText={setBody}
/>
<TouchableOpacity
style={{ justifyContent: "center", flex: 1, flexDirection: "row" }}
onPress={() => createCommunityPost({ body })}
>
<Text style={styles.sendText}>Send</Text>
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "flex-start",
backgroundColor: "#2B3654",
justifyContent: "flex-end",
},
inputContainer: {
justifyContent: "flex-end",
flexDirection: "row",
},
sendText: {
color: "white",
fontSize: 25,
},
input: {
fontSize: 25,
color: "white",
borderColor: "#2882D8",
borderWidth: 1,
borderRadius: 15,
width: "80%",
marginBottom: 30,
marginLeft: 10,
padding: 10,
},
});
export default Key;
解决方案
您必须KeyboardAvoidingView
由 react native 提供组件。
<KeyboardAvoidingView
behavior={Platform.OS == "ios" ? "padding" : "height"}
style={styles.container}
>
<SafeAreaView />
<CommunityPost />
<View style={styles.inputContainer}>
<TextInput
style={styles.input}
multiline={true}
placeholder="Type..."
placeholderTextColor="#CACACA"
value={body}
onChangeText={setBody}
/>
<TouchableOpacity
style={{ justifyContent: "center", flex: 1, flexDirection: "row" }}
onPress={() => createCommunityPost({ body })}
>
<Text style={styles.sendText}>Send</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
推荐阅读
- c# - 如何在 Docker 中将文件夹路径作为运行参数传递
- optimization - 在 Anylogic 中同时运行仿真和优化
- html - 具有类的第一个元素的 CSS
- c - 孩子内部的值修改不会更新
- google-people-api - 如何从 429 恢复资源已用尽(例如检查配额)
- angular - 检查数组值是否存在于订阅 angular10
- javascript - 我使用 .off() 限制一次点击。但是当下一页按钮不起作用时
- swift - 如何在我的 Xcode Swift 项目的不同导航堆栈中从一个视图控制器屏幕切换到另一个?
- docker - 如何将 docker 卷与来自 dockerhub 的 docker 映像一起使用
- google-analytics - 如何在nuxt js中添加全局站点标签以及如何发送事件?