React Native 键盘感知滚动视图和键盘避免视图不允许显示键盘

hmae6n7t  于 2022-12-30  发布在  React
关注(0)|答案(1)|浏览(179)

我正在做一个登录屏幕使用react原生和我试图使它完全像reddit登录屏幕,但当我使用KeyboardAwareScrollView和KeyboardAvoidingView键盘不出现。它不断打开和关闭。Login.js:
Expo SDK:46平台:机器人

<View style={{ flex: 1 }}>
  <KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : "height"}>
    <KeyboardAwareScrollView style={{ height: 500 }}>
      <View
        style={{
          paddingRight: 10,
          flex: 0.05,
          flexDirection: "row",
          alignItems: "center",
          justifyContent: "space-between",
        }}
      >
        <Appbar.BackAction onPress={() => navigation.navigate("Home")} />
        <Text style={{ fontSize: 24, fontWeight: "bold", color: "#ff4757" }}>
          WA
        </Text>
        <TouchableOpacity onPress={() => navigation.navigate("Regist")}>
          <Text style={{ color: "#ff4757" }}>Sign up</Text>
        </TouchableOpacity>
      </View>

      <View style={styles.centeredView}>
        <View style={{ flex: 0.9 }}>
          <Text style={styles.loginTitleText}>Log in to WearAble</Text>
          <View style={styles.continueWithGA}>
            <TouchableOpacity
              style={styles.buttonlogoStyle}
              activeOpacity={0.5}
            >
              <Image source={googleimg} style={styles.buttonImageIconStyle} />
              <Text style={styles.buttonTextStyle}>Continue with Google</Text>
            </TouchableOpacity>
            <TouchableOpacity
              style={styles.buttonlogoStyle}
              activeOpacity={0.5}
            >
              <Image source={appleimg} style={styles.buttonImageIconStyle} />
              <Text style={styles.buttonTextStyle}>Continue with Apple</Text>
            </TouchableOpacity>
          </View>
          <View
            style={{
              flexDirection: "row",
              justifyContent: "center",
              width: "100%",
              alignItems: "center",
              marginTop: 20,
              marginBottom: 25,
            }}
          >
            <View style={styles.line} />
            <Text
              style={{
                marginLeft: 13,
                marginRight: 13,
                fontSize: 15,
                fontWeight: "bold",
              }}
            >
              OR
            </Text>
            <View style={styles.line} />
          </View>
          <View style={{ marginBottom: 10 }}>
            <TextInput
              mode="outlined"
              label="Username"
              outlineColor="gray"
              activeOutlineColor="#ff4757"
              style={{ backgroundColor: "none" }}
            />
          </View>
          <View style={{ marginBottom: 10 }}>
            <TextInput
              mode="outlined"
              label="Password"
              secureTextEntry={!showPass}
              right={
                <TextInput.Icon
                  icon={!showPass ? "eye-off" : "eye"}
                  color={!showPass ? "black" : "lightblue"}
                  onPress={eyeShow}
                />
              }
              outlineColor="gray"
              activeOutlineColor="#ff4757"
              style={{ backgroundColor: "none" }}
            />
          </View>
          <TouchableOpacity>
            <Text>Forgot Password?</Text>
          </TouchableOpacity>
          <HelperText type="error" visible={errorCheck}>
            Your Username or Password was incorrect!
          </HelperText>
        </View>

        <TouchableOpacity style={{ flex: 0.1 }} onPress={() => {}}>
          <Text style={{ fontSize: 17 }}>
            By continuing, you agree to out{" "}
            <Text style={{ color: "#ff4757", textDecorationLine: "underline" }}>
              User Agreement
            </Text>{" "}
            and{" "}
            <Text style={{ color: "#ff4757", textDecorationLine: "underline" }}>
              Privacy Policy
            </Text>
            .
          </Text>
        </TouchableOpacity>
      </View>

      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <View style={{ flex: 0.07 }}>
          <TouchableOpacity
            style={styles.loginButton}
            onPress={() => LoginUser(email, pass)}
          >
            <Text style={styles.loginButtonText}>Continue</Text>
          </TouchableOpacity>
        </View>
      </TouchableWithoutFeedback>
    </KeyboardAwareScrollView>
  </KeyboardAvoidingView>
</View>;

我想页面的行为像reddit登录屏幕

vc6uscn9

vc6uscn91#

我解决了我的问题。通过将keyboardAvoidingView组件移动到只包含Continue按钮和KeyboardAwareScrollView的页面内容中,代码现在是这样的:

<View style={{ flex: 1, backgroundColor: "white", marginTop: 5 }}>
  <StatusBar color={"white"} />
  <View
    style={{
      paddingRight: 10,
      flex: 0.05,
      flexDirection: "row",
      alignItems: "center",
      justifyContent: "space-between",
    }}
  >
    <Appbar.BackAction onPress={() => navigation.navigate("Home")} />
    <Text style={{ fontSize: 24, fontWeight: "bold", color: "#ff4757" }}>
      WA
    </Text>
    <TouchableOpacity onPress={() => navigation.navigate("Regist")}>
      <Text style={{ color: "#ff4757" }}>Sign up</Text>
    </TouchableOpacity>
  </View>

  <View style={styles.centeredView}>
    <KeyboardAwareScrollView
      style={{ justifyContent: "space-between", backgroundColor: "green" }}
    >
      <View>
        <Text style={styles.loginTitleText}>Log in to WearAble</Text>
        <View style={styles.continueWithGA}>
          <TouchableOpacity style={styles.buttonlogoStyle} activeOpacity={0.5}>
            <Image source={googleimg} style={styles.buttonImageIconStyle} />
            <Text style={styles.buttonTextStyle}>Continue with Google</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.buttonlogoStyle} activeOpacity={0.5}>
            <Image source={appleimg} style={styles.buttonImageIconStyle} />
            <Text style={styles.buttonTextStyle}>Continue with Apple</Text>
          </TouchableOpacity>
        </View>
        <View
          style={{
            flexDirection: "row",
            justifyContent: "center",
            width: "100%",
            alignItems: "center",
            marginTop: 20,
            marginBottom: 25,
          }}
        >
          <View style={styles.line} />
          <Text
            style={{
              marginLeft: 13,
              marginRight: 13,
              fontSize: 15,
              fontWeight: "bold",
            }}
          >
            OR
          </Text>
          <View style={styles.line} />
        </View>

        <View style={{ marginBottom: 10 }}>
          <TextInput
            mode="outlined"
            label="Username"
            outlineColor="gray"
            activeOutlineColor="#ff4757"
            style={{ backgroundColor: "none" }}
            returnKeyType="done"
          />
        </View>
        <View style={{ marginBottom: 10 }}>
          <TextInput
            mode="outlined"
            label="Password"
            secureTextEntry={!showPass}
            right={
              <TextInput.Icon
                icon={!showPass ? "eye-off" : "eye"}
                color={!showPass ? "black" : "lightblue"}
                onPress={eyeShow}
              />
            }
            outlineColor="gray"
            activeOutlineColor="#ff4757"
            style={{ backgroundColor: "none" }}
            returnKeyType="done"
          />
        </View>

        <TouchableOpacity>
          <Text>Forgot Password?</Text>
        </TouchableOpacity>
        <HelperText type="error" visible={errorCheck}>
          Your Username or Password was incorrect!
        </HelperText>
      </View>

      <TouchableOpacity style={{}} onPress={() => {}}>
        <Text style={{ fontSize: 17 }}>
          By continuing, you agree to out{" "}
          <Text style={{ color: "#ff4757", textDecorationLine: "underline" }}>
            User Agreement
          </Text>{" "}
          and{" "}
          <Text style={{ color: "#ff4757", textDecorationLine: "underline" }}>
            Privacy Policy
          </Text>
          .
        </Text>
      </TouchableOpacity>
    </KeyboardAwareScrollView>
  </View>

  <KeyboardAvoidingView style={{ height: 60 }}>
    <TouchableOpacity
      style={styles.loginButton}
      onPress={() => LoginUser(email, pass)}
    >
      <Text style={styles.loginButtonText}>Continue</Text>
    </TouchableOpacity>
  </KeyboardAvoidingView>
</View>;

相关问题