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

前端之家收集整理的这篇文章主要介绍了React-Native学习笔记之:实现简单地登录页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 学习React Native一些时间了,想自己根据学习的知识开发个简单地独立地APP,今天开始第一步,实现登录界面功能

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

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

Login.js:

  1. import React,{Component} from 'react';
  2. import {
  3. AppRegistry,StyleSheet,Text,View,Navigator,TextInput,TouchableHighlight
  4. } from 'react-native';
  5. import MainPage from './MainPage'
  6. export default class Login extends Component {
  7. constructor(props) {
  8. super(props);
  9. }
  10.  
  11. render() {
  12. return (<View style={styles.container}>
  13. <View style={styles.item}><Text style={styles.textStyle}>用户帐号:</Text>
  14. <TextInput
  15. ref="inputLoginName"
  16. autoFocus={true}
  17. underlineColorAndroid="gray"
  18. placeholder="请输入用户名"
  19. clearTextOnFocus={true}
  20. clearButtonMode="while-editing"
  21. style={{flex: 1}}
  22. onChangeText={(input) => this.setState({username: input})}
  23. ></TextInput>
  24. </View>
  25. <View style={styles.item}><Text style={styles.textStyle}>用户密码:</Text>
  26. <TextInput
  27. ref="inputLoginPwd"
  28. underlineColorAndroid="gray"
  29. placeholder="请输入密码"
  30. clearTextOnFocus={true}
  31. clearButtonMode="while-editing"
  32. style={{flex: 1}}
  33. onChangeText={(input) => this.setState({userpwd: input})}></TextInput>
  34. </View>
  35. <TouchableHighlight style={styles.login}
  36. underlayColor='transparent'
  37. onPress={()=>this.loginInMainpage()}><Text
  38. style={styles.loginText}>登录</Text></TouchableHighlight>
  39. </View>)
  40. }
  41.  
  42. /** * 登录进入主页面 */
  43. loginInMainpage() {
  44. this.refs.inputLoginName.blur();
  45. this.refs.inputLoginPwd.blur();
  46. this.props.navigator.resetTo({
  47. component: MainPage,params: {
  48. logNmae: this.state.username,logPwd: this.state.userpwd,parentComponent: this,...this.props
  49. },});
  50. }
  51.  
  52. setLoginName(input) {
  53. this.setState = {inputName: input}
  54. }
  55.  
  56. setLoginPwd(input) {
  57. this.setState = {inputPwd: input}
  58. }
  59. }
  60. const styles = StyleSheet.create({
  61. container: {
  62. flex: 1,justifyContent: 'center'
  63. },item: {
  64. flexDirection: 'row',alignItems: 'center',margin: 10
  65. },textStyle: {
  66. fontSize: 18,color: 'black',marginRight: 10
  67. },login: {
  68. height: 40,backgroundColor: 'green',margin: 20,justifyContent: 'center',},loginText: {
  69. fontSize: 20,alignSelf: 'center',color: '#FFF'
  70. }
  71.  
  72. })

点击登录跳转到MainPage.js:

  1. import React,{Component} from 'react';
  2. import {
  3. AppRegistry,} from 'react-native';
  4. export default class MainPage extends Component {
  5. constructor(props) {
  6. super(props);
  7. }
  8.  
  9. render() {
  10. return (<View style={styles.container}>
  11. <Text style={styles.textStyle}>欢迎来到主界面</Text>
  12. <Text style={styles.textStyle}>您当前的用户名是:{this.props.logNmae}</Text>
  13. <Text style={styles.textStyle}>您当前的密码是:{this.props.logPwd}</Text>
  14. </View>)
  15. }
  16. }
  17. const styles = StyleSheet.create({
  18. container: {
  19. flex: 1,})

丑陋的效果图:

猜你在找的React相关文章