React-Native学习笔记之:实现简单地登录页面

 学习React Native一些时间了,想自己根据学习的知识开发个简单地独立地APP,今天开始第一步,实现登录界面功能:

index.ios.js或者index.android.js

import React, {Component} from 'react';
import {
    AppRegistry,
} from 'react-native';
import Welcome from './pages/Welcome'
AppRegistry.registerComponent('DeviceRnApp', () => Welcome);

Login.js:

import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Navigator,
    TextInput,
    TouchableHighlight
} from 'react-native';
import MainPage from './MainPage'
export default class Login extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (<View style={styles.container}>
            <View style={styles.item}><Text style={styles.textStyle}>用户帐号:</Text>
                <TextInput
                    ref="inputLoginName"
                    autoFocus={true}
                    underlineColorAndroid="gray"
                    placeholder="请输入用户名"
                    clearTextOnFocus={true}
                    clearButtonMode="while-editing"
                    style={{flex: 1}}
                    onChangeText={(input) => this.setState({username: input})}
                ></TextInput>
            </View>
            <View style={styles.item}><Text style={styles.textStyle}>用户密码:</Text>
                <TextInput
                    ref="inputLoginPwd"
                    underlineColorAndroid="gray"
                    placeholder="请输入密码"
                    clearTextOnFocus={true}
                    clearButtonMode="while-editing"
                    style={{flex: 1}}
                    onChangeText={(input) => this.setState({userpwd: input})}></TextInput>
            </View>
            <TouchableHighlight style={styles.login}
                                underlayColor='transparent'
                                onPress={()=>this.loginInMainpage()}><Text
                style={styles.loginText}>登录</Text></TouchableHighlight>
        </View>)
    }

    /**
     * 登录进入主页面
     */
    loginInMainpage() {
        this.refs.inputLoginName.blur();
        this.refs.inputLoginPwd.blur();
        this.props.navigator.resetTo({
            component: MainPage,
            params: {
                logNmae: this.state.username,
                logPwd: this.state.userpwd,
                parentComponent: this,
                ...this.props
            },
        });
    }

    setLoginName(input) {
        this.setState = {inputName: input}
    }

    setLoginPwd(input) {
        this.setState = {inputPwd: input}
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center'
    },
    item: {
        flexDirection: 'row',
        alignItems: 'center',
        margin: 10
    },
    textStyle: {
        fontSize: 18,
        color: 'black',
        marginRight: 10
    },
    login: {
        height: 40,
        backgroundColor: 'green',
        margin: 20,
        justifyContent: 'center',
    },
    loginText: {
        fontSize: 20,
        alignSelf: 'center',
        color: '#FFF'
    }

})

点击登录跳转到MainPage.js:

import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Navigator,
} from 'react-native';
export default class MainPage extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (<View style={styles.container}>
            <Text style={styles.textStyle}>欢迎来到主界面</Text>
            <Text style={styles.textStyle}>您当前的用户名是:{this.props.logNmae}</Text>
            <Text style={styles.textStyle}>您当前的密码是:{this.props.logPwd}</Text>
        </View>)
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    textStyle: {
        fontSize: 18,
        color: 'black',
        marginRight: 10
    },
})

丑陋的效果图:
这里写图片描述
这里写图片描述

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 好的,我可以回答这个问题。要用React编写登录界面,可以使用React组件和React Router来实现。首先,需要创建一个包含登录表单的组件,可以使用React的表单组件来实现。然后,使用React Router来创建一个路由,将登录界面与其他页面进行连接。最后,可以使用React的状态管理来处理用户登录信息。希望这个回答对你有帮助。 ### 回答2: 使用React编写一个登录界面需要以下几个步骤: 1. 创建一个React项目并初始化:首先,在命令行中使用`create-react-app`命令创建一个新的React项目,并进行初始化。 2. 创建组件:在项目的`src`目录下创建一个新的组件文件,命名为`Login.js`。在该文件中,定义一个名为`Login`的类组件,该组件将负责渲染登录界面的内容。 3. 设置组件状态:在`Login`组件的构造函数中,使用`state`对象或者`useState`Hook创建需要的状态变量,如用户名和密码。 4. 处理用户输入:创建两个输入框,分别用于输入用户名和密码,并为它们绑定相应的事件处理函数,以便在用户输入时更新相关状态变量。 5. 处理登录事件:创建一个登录按钮,并为其绑定一个点击事件处理函数。在该函数中,可以获取用户输入的用户名和密码,并进行验证。验证成功后,可以进行其他操作,如跳转到用户主页。 6. 渲染界面:在`render`方法中,使用JSX语法来描述登录界面的布局和内容。将输入框、登录按钮等元素放置在适当的位置,并将其与相应的状态变量关联起来。 7. 导出组件:将`Login`组件导出,以便在其他文件中引用和使用。 8. 在根组件中使用登录界面:在`App.js`(或其他根组件文件)中引入`Login`组件,并在`render`方法中将其作为子组件进行渲染。 9. 运行项目:在命令行中运行`npm start`命令,启动React应用,并在浏览器中查看登录界面。 以上是使用React编写一个简单的登录界面的基本步骤。当然,还可以根据具体需求对界面进行样式设计、添加验证功能等。 ### 回答3: 使用React编写一个登录界面可以通过以下步骤实现: 1. 创建一个React项目:首先,可以使用create-react-app来创建一个新的React项目。在终端中运行以下命令: ``` npx create-react-app login-app ``` 2. 设置登录组件:在src文件夹中创建一个Login.js文件,用于编写登录组件的代码。可以使用React的函数组件或者类组件来创建登录界面。例如,可以使用函数组件的形式来创建一个简单的登录表单: ```jsx import React from 'react'; function Login() { return ( <div> <h2>登录</h2> <form> <input type="text" placeholder="用户名" /> <input type="password" placeholder="密码" /> <button type="submit">登录</button> </form> </div> ); } export default Login; ``` 3. 在App组件中使用登录组件:打开src/App.js文件,将新创建的Login组件导入并在App组件中使用。例如: ```jsx import React from 'react'; import Login from './Login'; function App() { return ( <div className="App"> <Login /> </div> ); } export default App; ``` 4. 启动应用程序:在终端中运行以下命令以启动React应用程序: ``` npm start ``` 这将在浏览器中打开一个新的窗口,并显示登录界面。 这只是创建一个简单的登录界面的基本步骤,你可以根据自己的需求进行样式和功能的定制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值