React Native【实战范例】登录页(含密码显示隐藏)

c 复制代码
import React, { useState } from "react";
import {
  Button,
  StyleSheet,
  Text,
  TextInput,
  TouchableOpacity,
  View,
} from "react-native";
import {
  SafeAreaProvider,
  useSafeAreaInsets,
} from "react-native-safe-area-context";
const LoginForm = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [showPassword, setShowPassword] = useState(false);
  const handleLogin = () => {
    console.log("登录:", email, password);
  };
  return (
    <View style={styles.container}>
      <TextInput
        style={styles.inputStyle}
        placeholder="邮箱"
        keyboardType="email-address"
        autoCapitalize="none"
        onChangeText={setEmail}
        value={email}
      />
      <View style={styles.passwordContainer}>
        <TextInput
          style={styles.inputStyle}
          placeholder="密码"
          secureTextEntry={!showPassword}
          onChangeText={setPassword}
          value={password}
        />
        <TouchableOpacity
          style={styles.togglePasswordButton}
          onPress={() => setShowPassword(!showPassword)}
        >
          <Text style={styles.togglePasswordButtonText}>
            {showPassword ? "隐藏" : "显示"}
          </Text>
        </TouchableOpacity>
      </View>
      <Button title="登录" onPress={handleLogin} />
    </View>
  );
};
export default function HomeScreen() {
  const insets = useSafeAreaInsets();
  return (
    <SafeAreaProvider>
      <View
        style={{
          flex: 1,
          paddingTop: insets.top, // 顶部安全区域
          paddingBottom: insets.bottom, // 底部安全区域
        }}
      >
        <LoginForm />
      </View>
    </SafeAreaProvider>
  );
}
const styles = StyleSheet.create({
  container: {
    padding: 20,
  },
  inputStyle: {
    height: 40,
    borderColor: "gray",
    borderWidth: 1,
    marginVertical: 10,
    paddingHorizontal: 10,
    flex: 1,
  },
  passwordContainer: {
    flexDirection: "row",
    alignItems: "center",
  },
  togglePasswordButton: {
    height: 40,
    width: 60,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#2296f3",
  },
  togglePasswordButtonText: {
    color: "#fff",
  },
});
相关推荐
一个扣子9 小时前
降低 Android APK 体积:Hermes 的字节码格式与资源压缩
react native·字节码·构建优化·包体积优化·android性能·hermes·apk瘦身
cn_mengbei1 天前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js
祖国的好青年3 天前
VS Code 搭建 React Native 开发环境(Windows 实战指南)
android·windows·react native·react.js
一个扣子3 天前
性能面板解读:通过 Hermes Runtime 测量函数执行耗时
react native·chrome devtools·hermes·性能面板·函数耗时·performance api
一个扣子5 天前
Hermes 的 Android 与 iOS 平台差异化配置详解
react native·字节码·新架构·hermes·android配置·ios配置·平台差异
茅盾体5 天前
React Native
android·react native·react.js
一个扣子6 天前
多环境配置:开发/生产环境下 Hermes 的开启与关闭策略
react native·开发模式·多环境配置·生产模式·hermes·环境切换·构建配置
TechMasterPlus7 天前
Hermes 深度解析:React Native 高性能 JavaScript 引擎实践指南
javascript·react native·react.js
令人头秃的代码0_07 天前
React Native Bundle更新升级
react native
用户600071819108 天前
【翻译】React Native JSI 深度解析(第 3 篇):面向 JavaScript 开发者的 C++
react native