React Native:表单登录页面的简单布局

表单效果图:
在这里插入图片描述
这个登录页面,只要使用ReactNative的TextInput, View, StyleSheet, Text几个组件。

用为React Native组件内不能使用Form表单,如果不使用第三方UI库的话,自己动手封装表单了。

TextInput组件,实现输入框;

View,容器组件,页面布局用,可以把它当成html元素对应的div标签

Text,内容组件,对标html的span标签,在ReactNative中,一般是使用Text组件将文本内容包裹起来,

StyleSheet,创建样式

样式代码如下:

const styles = StyleSheet.create({
  flex: {
    display: 'flex',
    justifyContent: 'space-between',
    marginTop: 20,
  },
  borderStyle: {
    borderColor: '#cacaca',
    borderWidth: 1,
  },
  inputStyle: {
    width: '80%',
    height: 35,
    marginLeft: 'auto',
    marginRight: 'auto',
    marginTop: 10,
  },
  button: {
    backgroundColor: 'blue',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
  },
  buttonText: {
    color: '#ffffff',
    fontWeight: 'bold',
  },
});

表单部分使用的Flex弹性布局,在ReactNative中,默认主轴方向应该是垂直方向【目前测试中,主轴方向默认是垂直方向】。

完整代码:

import React, {useState} from 'react';
import {
  TextInput,
  View,
  StyleSheet,
  Text,
  TouchableHighlight,
  Button,
} from 'react-native';

const formDefaultData = {
  userName: null,
  password: null,
};
export default function Form() {
  const [formData, setFormData] = useState(formDefaultData);

  const onBtn = () => {
    console.log('formData', formData);
  };
  return (
    <View style={styles.flex}>
      <TextInput
        placeholder="用户名"
        onChange={value => {
          console.log('表单', value);
        }}
        style={[styles.inputStyle, styles.borderStyle]}
      />
      <TextInput
        secureTextEntry={true}
        placeholder="密码"
        style={[styles.inputStyle, styles.borderStyle]}
      />
      <View style={[styles.inputStyle, styles.borderStyle]}>
        <Button
          onPress={onBtn}
          title="登录"
          color="#841584"
          accessibilityLabel="Learn more about this purple button"
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  flex: {
    display: 'flex',
    justifyContent: 'space-between',
    marginTop: 20,
  },
  borderStyle: {
    borderColor: '#cacaca',
    borderWidth: 1,
  },
  inputStyle: {
    width: '80%',
    height: 35,
    marginLeft: 'auto',
    marginRight: 'auto',
    marginTop: 10,
  },
  button: {
    backgroundColor: 'blue',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
  },
  buttonText: {
    color: '#ffffff',
    fontWeight: 'bold',
  },
});

待完善。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

读心悦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值