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",
  },
});
相关推荐
wordbaby21 小时前
React Native + RNOH:一个 `lazyScreen()` 搞定 48 页面启动懒加载
前端·react native
wordbaby2 天前
React Native 压缩上传全链路方案:从架构设计到生产实践
前端·react native
沙漠2 天前
React Native-SyncFormatEdittext:用 JSI 实现零闪烁的实时文本格式化
前端·react native
野槐3 天前
React Native基础
javascript·react native·react.js
wordbaby4 天前
Axios 上传大文件崩溃:鸿蒙 RNOH 下 XHR 返回空响应头引发的"假失败"
前端·react native
wordbaby4 天前
React Native 列表分页实战:下拉刷新与上拉加载的工程化方案
前端·react native
wordbaby4 天前
脱离 Tab 栏的艺术:React Native 全屏子页面的导航架构实践
前端·react native·harmonyos
wordbaby4 天前
React Native 新架构落地鸿蒙:跨三端政务级应用的工程实践与深度复盘
前端·react native·harmonyos
武当王丶也4 天前
React Native App 内更新实践:从版本策略到 APK 下载和安装
android·javascript·react native
武当王丶也4 天前
React Native Turbo Module 实战:从 0 封装一个 PDA 扫码模块
android·前端·react native