android 键盘每次关闭

pdtvr36n  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(122)

我有一个问题,我的身份验证屏幕,当我试图把一些东西在我的文本输入我的键盘关闭。
有人可以帮助我,并告诉我,如果有一个错误,在我的代码。我试着改变我的原生屏幕依赖(版本3.10.1),它仍然是同样的问题。

  1. import { createStackNavigator } from '@react-navigation/stack';
  2. import React, { useState } from 'react';
  3. import { View, Text, TextInput, TouchableOpacity, Image, StyleSheet } from 'react-native';
  4. // Screens
  5. import MainContainerScreen from '../MainContainer';
  6. import RegisterScreen from './RegisterScreen';
  7. import ForgotPasswordScreen from './ForgotPasswordScreen';
  8. // Screen Names
  9. const MainContainerName = "Main";
  10. const registerName = "Inscription";
  11. const forgotPasswordName = "Mot de passe oublié";
  12. const Stack = createStackNavigator();
  13. export default function LoginPage() {
  14. const [email, setEmail] = useState('');
  15. const [password, setPassword] = useState('');
  16. const handleLogin = (navigation) => {
  17. // Redirection vers l'interface MainContainer (TabNavigator)
  18. navigation.navigate(MainContainerName);
  19. };
  20. const handleForgotPassword = (navigation) => {
  21. // Redirection vers la page de réinitialisation du mot de passe
  22. navigation.navigate(forgotPasswordName);
  23. };
  24. const handleFirstTimeLogin = (navigation) => {
  25. // Redirection vers un autre écran pour la première connexion
  26. navigation.navigate(registerName);
  27. };
  28. function Login({ navigation }) {
  29. return (
  30. <View style={styles.container}>
  31. <Image source={require('../../assets/icon.png')} style={styles.logo} />
  32. <TextInput
  33. placeholder="Adresse e-mail"
  34. value={email}
  35. onChangeText={setEmail}
  36. style={styles.input}
  37. />
  38. <TextInput
  39. placeholder="Mot de passe"
  40. value={password}
  41. onChangeText={setPassword}
  42. secureTextEntry
  43. style={styles.input}
  44. />
  45. <TouchableOpacity onPress={() => handleLogin(navigation)} style={styles.button}>
  46. <Text style={styles.buttonText}>Connexion</Text>
  47. </TouchableOpacity>
  48. <TouchableOpacity onPress={() => handleForgotPassword(navigation)}>
  49. <Text style={styles.forgotPasswordText}>Mot de passe oublié</Text>
  50. </TouchableOpacity>
  51. <TouchableOpacity onPress={() => handleFirstTimeLogin(navigation)}>
  52. <Text style={styles.firstTimeLoginText}>Première connexion</Text>
  53. </TouchableOpacity>
  54. </View>
  55. );
  56. }
  57. return (
  58. <Stack.Navigator>
  59. <Stack.Screen name="Login" component={Login} options={{ headerShown: false }}/>
  60. <Stack.Screen name={forgotPasswordName} component={ForgotPasswordScreen} options={{ headerShown: false }}/>
  61. <Stack.Screen name={registerName} component={RegisterScreen} options={{ headerShown: false }}/>
  62. <Stack.Screen name={MainContainerName} component={MainContainerScreen} options={{ headerShown: false }}/>
  63. </Stack.Navigator>
  64. );
  65. }
  66. const styles = StyleSheet.create({
  67. container: {
  68. flex: 1,
  69. alignItems: 'center',
  70. justifyContent: 'center',
  71. },
  72. logo: {
  73. width: 150,
  74. height: 150,
  75. marginBottom: 50,
  76. },
  77. input: {
  78. width: '80%',
  79. height: 40,
  80. borderWidth: 1,
  81. borderColor: 'gray',
  82. borderRadius: 5,
  83. marginBottom: 10,
  84. paddingHorizontal: 10,
  85. },
  86. button: {
  87. backgroundColor: 'blue',
  88. width: '80%',
  89. height: 40,
  90. alignItems: 'center',
  91. justifyContent: 'center',
  92. borderRadius: 5,
  93. marginBottom: 10,
  94. },
  95. buttonText: {
  96. color: 'white',
  97. fontSize: 16,
  98. },
  99. forgotPasswordText: {
  100. color: 'blue',
  101. marginBottom: 10,
  102. },
  103. firstTimeLoginText: {
  104. color: 'blue',
  105. },
  106. });
6mzjoqzu

6mzjoqzu1#

在您的代码中,您已经在堆栈组件中创建了Login组件,并在堆栈组件中声明了电子邮件和密码状态,因此当您更改电子邮件或密码状态时,堆栈组件将重新呈现,并与该Login组件沿着卸载(或完全销毁)并再次装载。这就是键盘消失的原因。因此,为了使代码正常工作,您可以将登录组件移出堆栈组件,并将密码和电子邮件状态以及所有导航处理程序移入登录组件。
更正后的代码如下所示。

  1. import { createStackNavigator } from "@react-navigation/stack";
  2. import React, { useState } from "react";
  3. import {
  4. View,
  5. Text,
  6. TextInput,
  7. TouchableOpacity,
  8. Image,
  9. StyleSheet,
  10. } from "react-native";
  11. import { NavigationContainer } from "@react-navigation/native";
  12. // Screens
  13. import MainContainerScreen from "./MainContainerScreen";
  14. import RegisterScreen from "./RegisterScreen";
  15. import ForgotPasswordScreen from "./ForgotPasswordScreen";
  16. // Screen Names
  17. const MainContainerName = "Main";
  18. const registerName = "Inscription";
  19. const forgotPasswordName = "Mot de passe oublié";
  20. const Stack = createStackNavigator();
  21. function Login({ navigation }) {
  22. const [email, setEmail] = useState("");
  23. const [password, setPassword] = useState("");
  24. const handleLogin = (navigation) => {
  25. // Redirection vers l'interface MainContainer (TabNavigator)
  26. navigation.navigate(MainContainerName);
  27. };
  28. const handleForgotPassword = (navigation) => {
  29. // Redirection vers la page de réinitialisation du mot de passe
  30. navigation.navigate(forgotPasswordName);
  31. };
  32. const handleFirstTimeLogin = (navigation) => {
  33. // Redirection vers un autre écran pour la première connexion
  34. navigation.navigate(registerName);
  35. };
  36. return (
  37. <View style={styles.container}>
  38. <Image source={require('../../assets/icon.png')} style={styles.logo} />
  39. <TextInput
  40. placeholder="Adresse e-mail"
  41. value={email}
  42. onChangeText={setEmail}
  43. style={styles.input}
  44. />
  45. <TextInput
  46. placeholder="Mot de passe"
  47. value={password}
  48. onChangeText={setPassword}
  49. secureTextEntry
  50. style={styles.input}
  51. />
  52. <TouchableOpacity
  53. onPress={() => handleLogin(navigation)}
  54. style={styles.button}
  55. >
  56. <Text style={styles.buttonText}>Connexion</Text>
  57. </TouchableOpacity>
  58. <TouchableOpacity onPress={() => handleForgotPassword(navigation)}>
  59. <Text style={styles.forgotPasswordText}>Mot de passe oublié</Text>
  60. </TouchableOpacity>
  61. <TouchableOpacity onPress={() => handleFirstTimeLogin(navigation)}>
  62. <Text style={styles.firstTimeLoginText}>Première connexion</Text>
  63. </TouchableOpacity>
  64. </View>
  65. );
  66. }
  67. export default function LoginPage() {
  68. return (
  69. <NavigationContainer>
  70. <Stack.Navigator>
  71. <Stack.Screen
  72. name="Login"
  73. component={Login}
  74. options={{ headerShown: false }}
  75. />
  76. <Stack.Screen
  77. name={forgotPasswordName}
  78. component={ForgotPasswordScreen}
  79. options={{ headerShown: false }}
  80. />
  81. <Stack.Screen
  82. name={registerName}
  83. component={RegisterScreen}
  84. options={{ headerShown: false }}
  85. />
  86. <Stack.Screen
  87. name={MainContainerName}
  88. component={MainContainerScreen}
  89. options={{ headerShown: false }}
  90. />
  91. </Stack.Navigator>
  92. </NavigationContainer>
  93. );
  94. }
  95. const styles = StyleSheet.create({
  96. container: {
  97. flex: 1,
  98. alignItems: "center",
  99. justifyContent: "center",
  100. },
  101. logo: {
  102. width: 150,
  103. height: 150,
  104. marginBottom: 50,
  105. },
  106. input: {
  107. width: "80%",
  108. height: 40,
  109. borderWidth: 1,
  110. borderColor: "gray",
  111. borderRadius: 5,
  112. marginBottom: 10,
  113. paddingHorizontal: 10,
  114. },
  115. button: {
  116. backgroundColor: "blue",
  117. width: "80%",
  118. height: 40,
  119. alignItems: "center",
  120. justifyContent: "center",
  121. borderRadius: 5,
  122. marginBottom: 10,
  123. },
  124. buttonText: {
  125. color: "white",
  126. fontSize: 16,
  127. },
  128. forgotPasswordText: {
  129. color: "blue",
  130. marginBottom: 10,
  131. },
  132. firstTimeLoginText: {
  133. color: "blue",
  134. },
  135. });
展开查看全部

相关问题