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",
  },
});
相关推荐
花椒技术3 天前
RN 多包热更新实践:更新校验、运行时加载与 Bridge 缓存治理
react native·react.js·harmonyos
互联网推荐官3 天前
上海 APP 开发服务甄选:技术架构设计、全维度判断框架
javascript·react native·react.js·app开发·开发经验·上海
墨狂之逸才6 天前
TRAE IDE 提效实战指南:少加班,多摸鱼
react native
墨狂之逸才7 天前
给 AI Coding Agent 装上 React Native 外挂:callstackincubator/agent-skills 上手指南
react native
墨狂之逸才7 天前
# React Native 人脸识别 UI 方案全对比:嵌入组件 · Activity · Dialog
react native
沙漠8 天前
ReactNative总结系列四 --- FlatList白屏卡顿优化
react native·性能优化
wordbaby9 天前
rn-cross-calendar:一个兼容 React 18/19、RN/RNOH 的跨平台日历组件
前端·react native·harmonyos
沙漠10 天前
ReactNative总结系列三 --- 性能优化
react native·性能优化
leeyi10 天前
Graph 编排:不只是 ReAct 的通用 DAG
react native·agent·graphql
不爱吃糖的程序媛11 天前
React Native 三方库 react-native-version-number 鸿蒙适配实战:从零到版本信息展示
react native·react.js·harmonyos