React Native实现注册和登录

2023-05-16

React-Native-Register-Login-Demo

这是最终的效果图

项目演示

项目Demo可以到github上下载,方便大家理解。


下面将一步一步带大家完成这个Demo

搭建环境

环境的搭建只需要安装React Native中文网的步骤搭建环境即可,可以选择不同的桌面系统对不同的手机系统的环境的搭建。
这是使用的是最新的0.51版本,想要切换版本的可以点击左上角的版本来切换所需的版本
RN版本选择

程序开发

引入依赖

我们的Demo中使用了navigation,所以就需要引入依赖

首先,编辑package.json文件,在”dependencies”中添加”react-navigation”: “git+https://github.com/react-community/react-navigation.git“,这样就引入了依赖,添加后的”dependencies”如下所示

"dependencies": {
  "react": "16.2.0",
  "react-native": "0.52.0",
  "react-navigation": "git+https://github.com/react-community/react-navigation.git"
},

然后,我们就需要安装依赖了,使用命令行(mac的终端,windows的cmd)先进入项目根目录,然后执行npm install命令,然后运行一次项目,如果出错,就需要先将node_modules文件夹删除,然后重新执行一次npm install命令,这样就准备完成了

集中代码

个人习惯将js代码放到同一个文件夹下面,这样方便管理代码,所以我们需要先自己创建一个scene文件夹,然后将App.js文件夹移到scene文件夹中

如果使用的是WebStorm编辑的项目的话,我们移动App.js文件之后,它会自动修改index.js文件,便不用执行下一步了(以防万一,还是打开index.js查看是否正确)

当我们移动了App.js文件之后,运行项目可能出错,这是因为index.js文件中的程序入口指向的是App.js,而在上一步更改了App.js的路径,所以这里需要将路径更改正确

import { AppRegistry } from 'react-native';
import App from './scene/App';

AppRegistry.registerComponent('Demo', () => App);

这就是修改过后的index.js文件内容,注意第二行代码的impoot指向的是scene下的App.js文件

界面搭建

到这里,准备工作就都做完了,现在开始编写界面的文件,因为这个工程用了三个界面,所以我们再新建三个js文件,分别为loginScene.js、registerScene.js、homeScene.js,三个不同的js文件显示三个不同的界面,最后再用navigation来相互跳转即可(后话)

因为界面的搭建非常简单,所以直接贴代码,如果还有不清楚的,可以参考如下几个链接
《Flex 布局教程:语法篇》——阮一峰
《Flex 布局教程:实例篇》——阮一峰
《React-Native 的基本控件属性方法》——冬瓜小生

LoginScene.js

import React, { Component } from 'react';
import {
    TouchableOpacity,
    StyleSheet,
    TextInput,
    View,
    Text,
    Alert,
    Button
} from 'react-native';

export default class LoginScene extends Component {
    render() {
        return (
            <TouchableOpacity  //用可点击的组件作为背景
                activeOpacity={1.0}  //设置背景被点击时的透明度改变值
                style={styles.container}>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        style={styles.input}
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'用户名'}  //设置占位符
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        style={styles.input}
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        secureTextEntry={true}  //设置为密码输入框
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'密码'}  //设置占位符
                    />
                </View>
                <TouchableOpacity
                    style={styles.button}>
                    <Text
                        style={styles.btText}>登录</Text>  //设置按钮值
                </TouchableOpacity>
                <TouchableOpacity
                    style={styles.button}>
                    <Text
                        style={styles.btText}>注册</Text>  //设置按钮值
                </TouchableOpacity>
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    input: {
        width: 200,
        height: 40,
        fontSize: 20,
        color: '#fff',//输入框输入的文本为白色
    },
    inputBox: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        width: 280,
        height: 50,
        borderRadius: 8,
        backgroundColor: '#58812F',
        marginBottom: 8,
    },
    button: {
        height: 50,
        width: 280,
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 8,
        backgroundColor: '#58812F',
        marginTop: 20,
    },
    btText: {
        color: '#fff',
        fontSize: 20,
    }
});

registerScene.js

import React, { Component } from 'react';
import {
    TouchableOpacity,
    StyleSheet,
    TextInput,
    View,
    Text,
    Alert
} from 'react-native';

export default class RegisterScene extends Component {
    render() {
        return (
            <TouchableOpacity
                activeOpacity={1.0}  //设置背景被点击时,透明度不变
                style={styles.container}>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        style={styles.input}
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'用户名'}  //设置占位符
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        style={styles.input}
                        secureTextEntry={true}  //设置为密码输入框
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'密码'}  //设置占位符
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        style={styles.input}
                        secureTextEntry={true}  //设置为密码输入框
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'确认密码'}  //设置占位符
                    />
                </View>
                <TouchableOpacity
                    style={styles.button}>
                    <Text
                        style={styles.btText}>注册</Text>
                </TouchableOpacity>
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    input: {
        width: 200,
        height: 40,
        fontSize: 20,
        color: '#fff',//输入框输入的文本为白色
    },
    inputBox: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        width: 280,
        height: 50,
        borderRadius: 8,
        backgroundColor: '#66f',
        marginBottom: 8,
    },
    button: {
        height: 50,
        width: 280,
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 8,
        backgroundColor: '#66f',
        marginTop: 20,
    },
    btText: {
        color: '#fff',
        fontSize: 20,
    }
});

homeScene.js

import React, { Component } from 'react';
import {
    Text,
    View,
    StyleSheet,
    Button
} from 'react-native';

export default class HomeScene extends Component {

    /**
     * 返回到登陆界面
     */
    backToLogin = () => {
    }

    render() {
        return (
            <View
                style={styles.container}>
                <Text
                    style={styles.content}
                >登录成功!这是主页!</Text>
                <Button
                    onPress={this.backToLogin}  //添加点击事件
                    style={styles.button}
                    title='点击返回登陆'/>  //设置按钮标题
            </View>
        );
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    content: {
        fontSize: 40,
    },
});

到这里,三个界面就搭建完成了,想看看效果,可以修改index.js来查看,比如试试查看登陆界面的效果,index.js文件修改如下

import { AppRegistry } from 'react-native';
// import App from './scene/App';
import LoginScene from './scene/loginScene.js'

AppRegistry.registerComponent('Demo', () => LoginScene);

现在,三个界面的现实效果分别如下

登陆界面
注册页面
主页

添加事件

我们的界面已经搭建完成了,但是点击按钮没有任何反应。所以,现在来添加一些事件。

输入框值改变事件

首先,要添加的是输入框的事件,我们需要接收到输入框输入的值,然后才能进行相应的操作。输入框可以添加的事件很多,我们这里需要的是获取输入框中的值,所以恰当的选择有两个,第一个是onChangeText,第二个是onEndEditing。两个都可以获取到输入结束之后输入框中的值,但是onEndEditing获取值比较麻烦,所以这里就用第一个了。
但还要思考一个问题,我们获取到的值怎么使用呢?React Native中的InputText中的值不能直接获取,必须通过其事件才能获取,所以我们只能创建几个属性来保存获取到的值,然后再去使用它们。所以需要对相应的代码进行修改了,我们在对应的类中增加代码如下(class内部,render()方法之外)。

loginScene.js

username = '';  //保存用户名
password = '';  //保存密码

/**
 * 当用户名输入框值改变时,保存改变的值
 * @param  {[type]} newUsername [输入的用户名]
 */
onUsernameChanged = (newUsername) => {
    console.log(newUsername);//运行后可以在输入框随意输入内容并且查看log验证!
    this.username = newUsername;
};

/**
 * 当密码输入框值改变时,保存改变的值
 * @param  {[type]} newUsername [输入的密码]
 */
onPasswordChanged = (newPassword) => {
    console.log(newPassword);//运行后可以在输入框随意输入内容并且查看log验证!
    this.password = newPassword;
};

还需要给每个输入框绑定事件,就需要添加一行代码onChangeText={this.onUsernameChanged},例如,登陆界面中的用户名输入框的代码更改后的结果

<View
    style={styles.inputBox}>
    <TextInput
        onChangeText={this.onUsernameChanged}  //添加值改变事件
        style={styles.input}
        autoCapitalize='none'  //设置首字母不自动大写
        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
        placeholderTextColor={'#ccc'}  //设置占位符颜色
        placeholder={'用户名'}  //设置占位符
    />
</View>

registerScene.js

username = '';  //保存用户名
password = '';  //保存密码
confirmPassword = '';  //保存确认密码

/**
 * 当用户名输入框值改变时,保存改变的值
 * @param  {[type]} newUsername [输入的用户名]
 */
onUsernameChanged = (newUsername) => {
    console.log(newUsername);//运行后可以在输入框随意输入内容并且查看log验证!
    this.username = newUsername;
};

/**
 * 当密码输入框值改变时,保存改变的值
 * @param  {[type]} newUsername [输入的密码]
 */
onPasswordChanged = (newPassword) => {
    console.log(newPassword);//运行后可以在输入框随意输入内容并且查看log验证!
    this.password = newPassword;
};

/**
 * 当确认密码输入框值改变时,保存改变的值
 * @param  {[type]} newUsername [输入的确认密码]
 */
onConfirmPasswordChanged = (newConfirmPassword) => {
    console.log(newConfirmPassword);//运行后可以在输入框随意输入内容并且查看log验证!
    this.confirmPassword = newConfirmPassword;
}

相应的事件可自己绑定

这时,我们再运行程序,在输入框中输入一些值,便可以在控制台中看到相应的输出了。

按钮事件

输入框的事件绑定完成之后,我们就需要给按钮添加事件了
我们的按钮用的都是TouchableOpacity(首页用的Button),它们都有一个onPress事件可以使用,所以就非常方便了,添加的方式类似,话不多说,直接上代码

loginScene.js

/**
 * 登陆按钮,点击时验证输入的用户名和密码是否正确,正确时进入主页面,否则弹出提示
 */
login = () => {
    if (this.username == 'Admin' && this.password == '123') {
        Alert.alert("登陆成功");  //弹出提示框
    } else {
        Alert.alert("登陆失败","用户名或密码不正确");  //弹出提示框
    }
};

/**
 * 注册按钮,点击进入注册界面
 */
register = () => {
}

“登陆”按钮添加事件后的代码

<TouchableOpacity
    onPress={this.login} //添加点击事件
    style={styles.button}>
    <Text
        style={styles.btText}>登录</Text>
</TouchableOpacity>

registerScene.js

/**
 * 注册按钮,根据输入的内容判断注册是否成功
 */
register = () => {
    if (this.username != '' && this.password != '') {
        if (this.username != 'Admin') {
            if (this.password === this.confirmPassword) {
                Alert.alert("注册成功","返回登陆")
            } else {
                Alert.alert("注册失败","密码与确认密码不同");
            }
        } else {
            Alert.alert("注册失败","此用户名已经被注册");
        }
    } else {
        Alert.alert("注册失败","用户名或密码不能为空");
    }
};

到这里,事件差不多都添加完成了,但是,我们别忘了我们用的是什么做的背景。。。翻上去看代码的瞬间明白了,哦,原来是TouchableOpacity,那这个是用来干嘛的呢。这就是React Native中的一些问题了,我们的输入框输入结束之后,怎么取消它的焦点呢。前面测试的时候可能就有人遇到这个问题:当输入过数据之后,我们想重载界面(mac的command+r,windows的rr)失效了。所以,我们需要做一件事,就是点击空白处让输入框失去焦点。所以,我们需要给输入框添加一个属性ref,这样,我们就可以通过ref来获取输入框,再调用其blur方法,就可以让它们失去焦点了。代码添加如下

loginScene.js

/**
 * 点击空白处使输入框失去焦点
 */
blurTextInput = () => {
    this.refs.username.blur();
    this.refs.password.blur();
}

......

render() {
        return (
            <TouchableOpacity  //用可点击的组件作为背景
                activeOpacity={1.0}  //设置背景被点击时的透明度改变值
                onPress={this.blurTextInput}  //添加点击事件
                style={styles.container}>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="username"  //设置描述
                      ......
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="password"  //设置描述
                      ......
                    />
                </View>
                ......
            </TouchableOpacity>
        );
    }

registerScene.js

/**
 * 点击空白处使输入框失去焦点
 */
blurTextInput = () => {
    this.refs.username.blur();
    this.refs.password.blur();
    this.refs.confirmPassword.blur();
}

......

render() {
        return (
            <TouchableOpacity
                activeOpacity={1.0}  //设置背景被点击时,透明度不变
                onPress={this.blurTextInput}  //添加点击事件
                style={styles.container}>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="username"  //添加描述
                        ......
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="password"  //添加描述
                        ......
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="confirmPassword"  //添加描述
                        ......
                    />
                </View>
                ......
            </TouchableOpacity>
        );
    }

现在,大家就可以点击按钮试试效果了(注意:登陆页面只有输入用户名为Admin并且密码为123时才会提示登陆成功,注册页面当用户名输入为Admin时会提示该用户已注册)

界面跳转

到这里,就是最后一步了,连接三个界面并可以相互跳转,这里就要用到我们最开始导入的navigation依赖了.
使用navigation时,需要先注册你需要使用到的界面,所以我们就需要修改App.js文件了

App.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { StackNavigator } from 'react-navigation';

//引入三个界面文件,并设置引入的类
import HomeScene from './homeScene';
import LoginScene from './loginScene';
import RegisterScene from './registerScene';

//注册导航界面
const SimpleApp = StackNavigator({
    Login: {
      screen: LoginScene,
      navigationOptions: {  // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
          headerTitle: '登陆',  //设置导航栏标题
      }
    },
    Home: {
      screen: HomeScene,
      navigationOptions: {  // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
          header: null,  //隐藏导航栏
      }
    },
    Register: {
      screen: RegisterScene,
      navigationOptions: {  // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
          headerTitle: '注册',  //设置导航栏标题
      }
    }
});

export default class App extends React.Component {
    render() {
        return <SimpleApp />;
    }
}

当这些界面都添加到navigation中之后,就可以通过navigation来使用它们了,所以我们只需要修改相应的按钮的点击事件即可。

loginScene.js

/**
 * 登陆按钮,点击时验证输入的用户名和密码是否正确,正确时进入主页面,否则弹出提示
 */
login = () => {
    if (this.username == 'Admin' && this.password == '123') {
        this.refs.username.blur();
      this.refs.password.blur();
        const { navigate } = this.props.navigation;  //获取navigation的navigate方法
        navigate('Home');  //跳转到注册过的Home界面
    } else {
        Alert.alert("登陆失败","用户名或密码不正确");  //弹出提示框
    }
};

/**
 * 注册按钮,点击进入注册界面
 */
register = () => {
    const { navigate } = this.props.navigation;  //获取navigation的navigate方法
    navigate('Register');  //跳转到注册过的Register界面
}

register.js

/**
 * 注册按钮,根据输入的内容判断注册是否成功
 */
register = () => {
    if (this.username != '' && this.password != '') {
        if (this.username != 'Admin') {
            if (this.password === this.confirmPassword) {
                const { goBack } = this.props.navigation;  //获取navigation的goBack方法

                Alert.alert("注册成功","返回登陆",[{text: '确定', onPress: () => { goBack(); }}])  //给弹出的提示框添加事件
            } else {
                Alert.alert("注册失败","密码与确认密码不同");
            }
        } else {
            Alert.alert("注册失败","此用户名已经被注册");
        }
    } else {
        Alert.alert("注册失败","用户名或密码不能为空");
    }
};

homeScene.js

/**
 * 返回到登陆界面
 */
backToLogin = () => {
    const { goBack } = this.props.navigation; //获取navigation的goBack方法
    goBack();  //返回上一界面
}

到这里,就完成了整个Demo的所有实现,需要看完整代码的可以将Demo下载下来。
项目的github链接

项目发布

项目发布可以分别发布到iOS端和android端,在React Native中文网也有相应的教程,只不过发布到iOS平台还需要有付费的开发者账号,才能将App上架到App Store;而android端就不需要开发者账号了,只需要生成一个属于自己的签名即可。

发布的步骤可以参考如下:

iOS端
android端
由于React Native中文网中对iOS平台发布的描述很少,所以这里还有一篇教程可供大家参考
iOS端参考

最后,这个项目打包后的安卓apk如下
[apk下载链接](链接: https://pan.baidu.com/s/1i5Znq6p) 密码: i4zu

最后再贴下所有的代码吧。

index.js

import { AppRegistry } from 'react-native';
import App from './scene/App';

AppRegistry.registerComponent('Demo', () => App);

App.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { StackNavigator } from 'react-navigation';

//引入三个界面文件,并设置引入的类
import HomeScene from './homeScene';
import LoginScene from './loginScene';
import RegisterScene from './registerScene';

//注册导航界面
const SimpleApp = StackNavigator({
    Login: {
      screen: LoginScene,
      navigationOptions: {  // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
          headerTitle: '登陆',  //设置导航栏标题
      }
    },
    Home: {
      screen: HomeScene,
      navigationOptions: {  // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
          header: null,  //隐藏导航栏
      }
    },
    Register: {
      screen: RegisterScene,
      navigationOptions: {  // 屏幕导航的默认选项, 也可以在组件内用 static navigationOptions 设置(会覆盖此处的设置)
          headerTitle: '注册',  //设置导航栏标题
      }
    }
});

export default class App extends React.Component {
    render() {
        return <SimpleApp />;
    }
}

loginScene.js

import React, { Component } from 'react';
import {
    TouchableOpacity,
    StyleSheet,
    TextInput,
    View,
    Text,
    Alert,
    Button
} from 'react-native';

export default class LoginScene extends Component {

    username = '';  //保存用户名
    password = '';  //保存密码

    /**
     * 当用户名输入框值改变时,保存改变的值
     * @param  {[type]} newUsername [输入的用户名]
     */
    onUsernameChanged = (newUsername) => {
        console.log(newUsername);//运行后可以在输入框随意输入内容并且查看log验证!
        this.username = newUsername;
    };

    /**
     * 当密码输入框值改变时,保存改变的值
     * @param  {[type]} newUsername [输入的密码]
     */
    onPasswordChanged = (newPassword) => {
        console.log(newPassword);//运行后可以在输入框随意输入内容并且查看log验证!
        this.password = newPassword;
    };

    /**
     * 点击空白处使输入框失去焦点
     */
    blurTextInput = () => {
        this.refs.username.blur();
        this.refs.password.blur();
    }

    /**
     * 登陆按钮,点击时验证输入的用户名和密码是否正确,正确时进入主页面,否则弹出提示
     */
    login = () => {
        if (this.username == 'Admin' && this.password == '123') {
            this.refs.username.blur();
            this.refs.password.blur();
            const { navigate } = this.props.navigation;  //获取navigation的navigate方法
            navigate('Home');  //跳转到注册过的Home界面
        } else {
            Alert.alert("登陆失败","用户名或密码不正确");  //弹出提示框

        }
    };

    /**
     * 注册按钮,点击进入注册界面
     */
    register = () => {
        const { navigate } = this.props.navigation;  //获取navigation的navigate方法
        navigate('Register');  //跳转到注册过的Register界面
    }

    render() {
        return (
            <TouchableOpacity  //用可点击的组件作为背景
                activeOpacity={1.0}  //设置背景被点击时的透明度改变值
                onPress={this.blurTextInput}  //添加点击事件
                style={styles.container}>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="username"  //设置描述
                        onChangeText={this.onUsernameChanged}  //添加值改变事件
                        style={styles.input}
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'用户名'}  //设置占位符
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="password"  //设置描述
                        onChangeText={this.onPasswordChanged}  //添加值改变事件
                        style={styles.input}
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        secureTextEntry={true}  //设置为密码输入框
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'密码'}  //设置占位符
                    />
                </View>
                <TouchableOpacity
                    onPress={this.login} //添加点击事件
                    style={styles.button}>
                    <Text
                        style={styles.btText}>登录</Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={this.register}  //添加点击事件
                    style={styles.button}>
                    <Text
                        style={styles.btText}>注册</Text>
                </TouchableOpacity>
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    input: {
        width: 200,
        height: 40,
        fontSize: 20,
        color: '#fff',//输入框输入的文本为白色
    },
    inputBox: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        width: 280,
        height: 50,
        borderRadius: 8,
        backgroundColor: '#58812F',
        marginBottom: 8,
    },
    button: {
        height: 50,
        width: 280,
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 8,
        backgroundColor: '#58812F',
        marginTop: 20,
    },
    btText: {
        color: '#fff',
        fontSize: 20,
    }
});

registerScene.js

import React, { Component } from 'react';
import {
    TouchableOpacity,
    StyleSheet,
    TextInput,
    View,
    Text,
    Alert
} from 'react-native';

export default class RegisterScene extends Component {

    username = '';  //保存用户名
    password = '';  //保存密码
    confirmPassword = '';  //保存确认密码

    /**
     * 当用户名输入框值改变时,保存改变的值
     * @param  {[type]} newUsername [输入的用户名]
     */
    onUsernameChanged = (newUsername) => {
        console.log(newUsername);//运行后可以在输入框随意输入内容并且查看log验证!
        this.username = newUsername;
    };

    /**
     * 当密码输入框值改变时,保存改变的值
     * @param  {[type]} newUsername [输入的密码]
     */
    onPasswordChanged = (newPassword) => {
        console.log(newPassword);//运行后可以在输入框随意输入内容并且查看log验证!
        this.password = newPassword;
    };

    /**
     * 当确认密码输入框值改变时,保存改变的值
     * @param  {[type]} newUsername [输入的确认密码]
     */
    onConfirmPasswordChanged = (newConfirmPassword) => {
        console.log(newConfirmPassword);//运行后可以在输入框随意输入内容并且查看log验证!
        this.confirmPassword = newConfirmPassword;
    }

    /**
     * 点击空白处使输入框失去焦点
     */
    blurTextInput = () => {
        this.refs.username.blur();
        this.refs.password.blur();
        this.refs.confirmPassword.blur();
    }

    /**
     * 注册按钮,根据输入的内容判断注册是否成功
     */
    register = () => {
        if (this.username != '' && this.password != '') {
            if (this.username != 'Admin') {
                if (this.password === this.confirmPassword) {
                    const { goBack } = this.props.navigation;  //获取navigation的goBack方法

                    Alert.alert("注册成功","返回登陆",[{text: '确定', onPress: () => { goBack(); }}])  //给弹出的提示框添加事件
                } else {
                    Alert.alert("注册失败","密码与确认密码不同");
                }
            } else {
                Alert.alert("注册失败","此用户名已经被注册");
            }
        } else {
            Alert.alert("注册失败","用户名或密码不能为空");
        }
    };

    render() {
        return (
            <TouchableOpacity
                activeOpacity={1.0}  //设置背景被点击时,透明度不变
                onPress={this.blurTextInput}  //添加点击事件
                style={styles.container}>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="username"  //添加描述
                        onChangeText={this.onUsernameChanged}  //添加值改变事件
                        style={styles.input}
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'用户名'}  //设置占位符
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="password"  //添加描述
                        onChangeText={this.onPasswordChanged}  //添加值改变事件
                        style={styles.input}
                        secureTextEntry={true}  //设置为密码输入框
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'密码'}  //设置占位符
                    />
                </View>
                <View
                    style={styles.inputBox}>
                    <TextInput
                        ref="confirmPassword"  //添加描述
                        onChangeText={this.onConfirmPasswordChanged}  //添加值改变事件
                        style={styles.input}
                        secureTextEntry={true}  //设置为密码输入框
                        autoCapitalize='none'  //设置首字母不自动大写
                        underlineColorAndroid={'transparent'}  //将下划线颜色改为透明
                        placeholderTextColor={'#ccc'}  //设置占位符颜色
                        placeholder={'确认密码'}  //设置占位符
                    />
                </View>
                <TouchableOpacity
                    onPress={this.register}  //添加点击事件
                    style={styles.button}>
                    <Text
                        style={styles.btText}>注册</Text>
                </TouchableOpacity>
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    input: {
        width: 200,
        height: 40,
        fontSize: 20,
        color: '#fff',//输入框输入的文本为白色
    },
    inputBox: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        width: 280,
        height: 50,
        borderRadius: 8,
        backgroundColor: '#66f',
        marginBottom: 8,
    },
    button: {
        height: 50,
        width: 280,
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 8,
        backgroundColor: '#66f',
        marginTop: 20,
    },
    btText: {
        color: '#fff',
        fontSize: 20,
    }
});

homeScene.js

import React, { Component } from 'react';
import {
    Text,
    View,
    StyleSheet,
    Button
} from 'react-native';


export default class HomeScene extends Component {

    /**
     * 返回到登陆界面
     */
    backToLogin = () => {
        const { goBack } = this.props.navigation; //获取navigation的goBack方法
        goBack();  //返回上一界面
    }

    render() {
        return (
            <View
                style={styles.container}>
                <Text
                    style={styles.content}
                >登录成功!这是主页!</Text>
                <Button
                    onPress={this.backToLogin}  //添加点击事件
                    style={styles.button}
                    title='点击返回登陆'/>  //设置按钮标题
            </View>
        );
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    content: {
        fontSize: 40,
    },
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Native实现注册和登录 的相关文章

  • UICollectionViewCell 自动大小的两种常用方式

    方法一 xff1a 自动计算 override func viewDidLoad super viewDidLoad if let flowLayout 61 collectionView collectionViewLayout as U
  • UITableViewCell 图片自适应

    常见的一种方法是异步Completed时 xff0c 根据图片大小计算cell的高度并缓存到字典里后 xff0c 刷新tableView或indexPath 但这里介绍另一种更好的方式是使用约束处理 xff0c 对imageView的上下左
  • Swift编译死锁问题

    最近在Swift OC混编项目里遇到个奇怪的问题 xff0c 这样一行代码尽然引发了Swift编译过程死锁 xxSwiftModel salary 61 xxOCModel salary doubleValue 如果哪位大神知道根因 xff
  • 多线程及聊天室程序

    1 一个多线程程序 新建一个 win32 console application 取名 xff1a MultiThread 选空的工程 xff0c 并建立一个名为 MultiThread 的源文件编辑 xff1a include inclu
  • SQL Server 2008语句大全完整版

    61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 设置内存选项 61 61 61 61 61 61 61 61 61 61 61 61 61 61
  • Swift从相册选择图片,图文混排并且可以保存、上传数据

    博主最近突发奇想想做一个自己的日记本App xff0c 在过程中遇到了一些坑 xff0c 摸索了很久才做出一个简单的日记本功能 先来看看一下效果吧 xff1a 先来说说这次用到的一些东西吧 xff1a 1 UIImagePickerCont
  • 2022年ABC模块样题十套分享

    2022年ABC模块样题十套分享 样题分享传送门
  • C# WinForm基础

    1 WinForm基础 Form1 cs using System using System Collections Generic using System ComponentModel using System Data using S
  • 做独立开发的一些感想

    好久没在CSDN上写东西 xff0c 这次写点感想吧 想想自己独立开发也有好多年了 xff0c 从刚毕业做的在线销售系统 xff0c 再到spring boot做写的my finances xff0c 再后来转iOS做的万能输入法 我的浏览
  • Vue3之script-setup全面解析

    可能很多同学 xff08 包括我 xff09 刚上手 Vue 3 0 之后 xff0c 都会觉得开发过程似乎变得更繁琐了 xff0c Vue 官方团队当然不会无视群众的呼声 xff0c 如果你基于脚手架和 vue 文件开发 xff0c 那么
  • windows下hadoop的部署和使用

    本教程在windows10环境下安装配置hadoop 43 hbase 43 spark 一 下载压缩包 xff1a 1 JavaJDK xff0c 1 8以上 2 Hadoop2 8 3 xff0c 下载地址 xff1a http arc
  • VS2019 C++的跨平台开发——Cmake项目(二)

    这一篇主要补充Windows下WSL xff08 Linux 子系统 xff09 创建 以及在WSL和远程Linux系统下编译和调试CMake项目 Widnows下创建WSL 1 右键左下角的Windows图标 61 应用和功能 61 启用
  • Ubuntu 10.10 "无线网络已经禁用” “wiress is disabled” 解决方法

    今天高高兴兴用真机器装好了Ubuntu 10 10 想WIFI 结果显示 无线网络已经禁用 wiress is disabled 我晕 xff0c LiveCD 启动没有任何问题 上了baidu Google 查了很多办法 iwlist s
  • C++ 出现两个头文件相互包含的解决办法

    综合网络信息 xff1a 相互引用解决办法 1 A导入B头文件 xff0c B不导入A头文件 采用 extern 方式引用 2 前向声明加指针 声明各种类的头文件 Classes h 前向声明 class User 放到前面的类要用指针 c
  • 双系统安装 ubuntu 22.04 + win11(yoga 14s 2021)

    考虑到无法完全转向 windows xff0c 于是选择双系统安装 ubuntu 文章安装过程主要参考 Ref 1 xff0c 具体细节会在文中体现 制作启动盘新建硬盘分区安装 ubuntu参考文献 制作启动盘 镜像下载 参考 Ubuntu
  • windows curl 之 invalid character ‘\‘‘ looking for beginning of value

    在windows环境下 xff0c 使用Windows Terminal执行 curl H 34 Content Type application json 34 X POST d 39 34 value 34 34 captcha 61
  • 多List集合总结 交集 并集 差集 去重复并集

    我们经常会遇到一些需求求集合的交集 差集 并集 例如下面两个集合 List lt String gt list1 61 new ArrayList lt String gt list1 add 34 A 34 list1 add 34 B
  • Swift4.0 Codable踩坑之派生类数据的保存

    本以为之前使用Codable的过程中踩的坑已经够多了 xff0c 今天博主有遇到一个坑 xff0c 调了一个下午才解决 xff0c 问题不大 xff0c 但是中文的技术文里都很少涉及这个问题 问题描述 xff1a 遵循了Codable协议的
  • Debian时间同步

    前言 一般做服务器集群的时候 xff0c 需要保证系统时间的同步了 xff0c 而在Linux上有一个工具可以很方便的进行同步 xff0c 那就是ntpdate 安装 安装很简单 xff0c 输入以下命令即可 xff1a span clas
  • Object-c学习笔记十八-----NSPredicate

    Cocoa提供了一个类NSPredicate类 xff0c 该类主要用于指定过滤器的条件 xff0c 该对象可以准确的描述所需条件 xff0c 对每个对象通过谓词进行筛选 xff0c 判断是否与条件相匹配 谓词表示计算真值或假值的函数 NS

随机推荐