ReactNative自定义组件3-个人资料页面

ReactNative自定义组件3-个人资料页面

导言

 最近做app,记录下自己写的组件

组件:个人资料页面

import React, { Component, useState }  from 'react';
import {StyleSheet, Text, View, Dimensions, Image, Button, TouchableOpacity, ImageBackground, TextInput, ScrollView} from 'react-native';
import PropTypes from 'prop-types';
import {BackgroundImage} from 'react-native-elements/dist/config';
// import {Input} from 'react-native-elements';
import {pxToDp} from './styleKits';

const screenWidth = Math.round(Dimensions.get('window').width);
const screenHeight = Math.round(Dimensions.get('window').height);

export default class PeopleInformation extends Component {
   constructor() {
       super();
       this.state = {
           isEditting:             false,

           name:                   '胡诗杰',
           head:                   require('./pic/head.jpg'),
           phoneNumber:            '13849489395',
           carType:                '奔驰',
           carMode:                '豪华型',
           emergencyPhoneNumber:   '17398508012',
           licensePlateNumber:     '豫8888',
           carColor:               '黑',

           onHeadPress:            PropTypes.func,
           onEditInformation:      PropTypes.func,
           onSavePress:            PropTypes.func,
           onCancelPress:          PropTypes.func,

           formPhoneNumber:            '13849489395',
           formCarType:                '奔驰',
           formCarMode:                '豪华型',
           formEmergencyPhoneNumber:   '17398508012',
           formLicensePlateNumber:     '豫8888',
           formCarColor:               '黑',
       }
       this.headPress = this.headPress.bind(this);
       this.editInformationPress = this.editInformationPress.bind(this);
       this.savePress = this.savePress.bind(this);
       this.cancelPress = this.cancelPress.bind(this);
   }

   headPress() {
       alert('function is null')
   };

   editInformationPress() {
       this.setState({
           isEditting:             true,
       })
   };

   savePress() {
       this.setState({
           isEditting:             false,

           phoneNumber:            this.state.formPhoneNumber == '' ? this.state.phoneNumber : this.state.formPhoneNumber,
           carType:                this.state.formCarType == '' ? this.state.carType : this.state.formCarType,
           carMode:                this.state.formCarMode == '' ? this.state.carMode : this.state.formCarMode,
           emergencyPhoneNumber:   this.state.formEmergencyPhoneNumber == '' ? this.state.emergencyPhoneNumber : this.state.formEmergencyPhoneNumber,
           licensePlateNumber:     this.state.formLicensePlateNumber == '' ? this.state.licensePlateNumber : this.state.formLicensePlateNumber,
           carColor:               this.state.formCarColor == '' ? this.state.carColor : this.state.formCarColor,
       })
   };

   cancelPress() {
       this.setState({
           isEditting:                 false,

           formPhoneNumber:            this.state.phoneNumber,
           formCarType:                this.state.carType,
           formCarMode:                this.state.carMode,
           formEmergencyPhoneNumber:   this.state.emergencyPhoneNumber,
           formLicensePlateNumber:     this.state.licensePlateNumber,
           formCarColor:               this.state.carColor,
       })
   };

   render() {
       return(
           <View style={{
               height: '100%',
               alignItems: 'center',
               backgroundColor: '#BFD0DA',
           }}>
               <View style={{
                   height: pxToDp(165),
                   width: '95%',
                   backgroundColor: 'white',
                   marginTop: '5%',
                   padding: 12,
                   elevation: 5,
                   borderRadius: 10,
                   shadowColor: '#303133',
               }}>
                   <View style={{
                       height: '55%',
                       width: '80%',
                       marginLeft: '10%',
                       flexDirection: 'row',
                   }}>
                       <Image source={this.state.head} style={{
                           height: pxToDp(80),
                           width: pxToDp(80),
                           borderRadius: 120,
                       }}/>
                       <View style={{
                           marginTop: '8%',
                           marginLeft: '5%',
                       }}>
                           <Text style={{
                               color: '#909399',
                           }}>{'姓名:' + this.state.name}</Text>
                           <Text style={{
                               color: '#909399',
                           }}>{'手机号:' + this.state.phoneNumber}</Text>
                       </View>
                   </View>
                   <View style={{
                       width: '80%',
                       height: 1,
                       marginTop: '5%',
                       alignSelf:'center',
                       opacity: 0.3,
                       backgroundColor: '#606266',
                   }} />
                   <View style={{
                       marginTop: '5%',
                       alignSelf: 'center',
                       flexDirection: 'row',
                   }}>
                       <TouchableOpacity
                           onPress={this.headPress}
                           style={{
                               alignSelf: 'center',
                               flexDirection: 'row',
                       }}>
                           <Image source={require('./pic/points.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                           }}/>
                           <Text style={{
                               marginLeft: '3%',
                               color: '#409EFF'
                           }}>{'更改头像'}</Text>
                       </TouchableOpacity>
                       <TouchableOpacity
                           onPress={this.editInformationPress}
                           style={{
                               marginLeft: '15%',
                               alignSelf: 'center',
                               flexDirection: 'row',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                           }}/>
                           <Text style={{
                               marginLeft: '3%',
                               color: '#409EFF'
                           }}>{'编辑信息'}</Text>
                       </TouchableOpacity>
                   </View>
               </View>
               <View style={{
                   height: '70%',
                   width: '95%',
                   padding: 10,
                   marginTop: '5%',
                   elevation: 5,
                   borderRadius: 10,
                   shadowColor: '#303133',
                   backgroundColor: 'white',
               }}>
                   <ScrollView style={{
                       height: 100,
                       overflow: 'scroll',
                   }}>
                       <View style={{
                           flexDirection: 'row',
                           alignItems: 'center',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                               marginLeft: '5%',
                           }}/>
                           <Text>{'手机号:'}</Text>
                           <TextInput
                               placeholder={this.state.formPhoneNumber}
                               editable={false}
                               selectionColor={'#409EFF'}
                               onChangeText={
                                   text => this.setState({
                                       formPhoneNumber: text
                                   })
                               }
                               value={this.state.formPhoneNumber}
                               style={{
                                   width: '60%',
                               }}
                           />
                       </View>
                       <View style={{
                           width: '90%',
                           height: 1,
                           alignSelf:'center',
                           opacity: 0.3,
                           backgroundColor: '#606266',
                       }} />
                       <View style={{
                           flexDirection: 'row',
                           alignItems: 'center',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                               marginLeft: '5%',
                           }}/>
                           <Text>{'汽车车型:'}</Text>
                           <TextInput
                               placeholder={this.state.formCarMode}
                               editable={false}
                               selectionColor={'#409EFF'}
                               onChangeText={
                                   text => this.setState({
                                       formCarMode: text
                                   })
                               }
                               value={this.state.formCarMode}
                               style={{
                                   width: '60%',
                               }}
                           />
                       </View>
                       <View style={{
                           width: '90%',
                           height: 1,
                           alignSelf:'center',
                           opacity: 0.3,
                           backgroundColor: '#606266',
                       }} />
                       <View style={{
                           flexDirection: 'row',
                           alignItems: 'center',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                               marginLeft: '5%',
                           }}/>
                           <Text>{'汽车型号:'}</Text>
                           <TextInput editable={this.state.isEditting}
                                      selectionColor={'#409EFF'}
                                      onChangeText={
                                          text => this.setState({
                                              formCarType: text
                                          })
                                      }
                                      value={this.state.formCarType}
                                      style={{
                                          width: '60%',
                                      }}
                           />
                       </View>
                       <View style={{
                           width: '90%',
                           height: 1,
                           alignSelf:'center',
                           opacity: 0.3,
                           backgroundColor: '#606266',
                       }} />
                       <View style={{
                           flexDirection: 'row',
                           alignItems: 'center',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                               marginLeft: '5%',
                           }}/>
                           <Text>{'紧急联系人:'}</Text>
                           <TextInput
                               placeholder={this.state.formEmergencyPhoneNumber}
                               editable={this.state.isEditting}
                               selectionColor={'#409EFF'}
                               onChangeText={
                                   text => this.setState({
                                       formEmergencyPhoneNumber: text
                                   })
                               }
                               value={this.state.formEmergencyPhoneNumber}
                               style={{
                                   width: '60%',
                               }}
                           />
                       </View>
                       <View style={{
                           width: '90%',
                           height: 1,
                           alignSelf:'center',
                           opacity: 0.3,
                           backgroundColor: '#606266',
                       }} />
                       <View style={{
                           flexDirection: 'row',
                           alignItems: 'center',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                               marginLeft: '5%',
                           }}/>
                           <Text>{'车辆牌照:'}</Text>
                           <TextInput
                               placeholder={this.state.formLicensePlateNumber}
                               editable={false}
                               selectionColor={'#409EFF'}
                               onChangeText={
                                   text => this.setState({
                                       formLicensePlateNumber: text
                                   })
                               }
                               value={this.state.formLicensePlateNumber}
                               style={{
                                   width: '60%',
                               }}
                           />
                       </View>
                       <View style={{
                           width: '90%',
                           height: 1,
                           alignSelf:'center',
                           opacity: 0.3,
                           backgroundColor: '#606266',
                       }} />
                       <View style={{
                           flexDirection: 'row',
                           alignItems: 'center',
                       }}>
                           <Image source={require('./pic/where.png')} style={{
                               height: pxToDp(20),
                               width: pxToDp(20),
                               marginLeft: '5%',
                           }}/>
                           <Text>{'车辆颜色:'}</Text>
                           <TextInput
                               placeholder={this.state.formCarColor}
                               editable={this.state.isEditting}
                               selectionColor={'#409EFF'}
                               onChangeText={
                                   text => this.setState({
                                       formCarColor: text
                                   })
                               }
                               value={this.state.formCarColor}
                               style={{
                                   width: '60%',
                               }}
                           />
                       </View>
                       <View style={{
                           width: '90%',
                           height: 1,
                           alignSelf:'center',
                           opacity: 0.3,
                           backgroundColor: '#606266',
                       }} />
                   </ScrollView>
                   <View style={{
                       height: '10%',
                   }}></View>
                   <View style={{
                       flexDirection: 'row',
                       alignItems: 'center',
                   }}>
                       <TouchableOpacity
                           onPress={this.savePress} style={{
                           height: pxToDp(40),
                           width: pxToDp(100),
                           marginLeft: '14%',
                           flexDirection: 'row',
                           alignItems: 'center',
                           display: this.state.isEditting?'flex':'none',
                       }}>
                           <Image source={require('./pic/points.png')} style={{
                               marginLeft: '20%',
                               height: pxToDp(20),
                               width: pxToDp(20),
                           }}/>
                           <Text style={{
                               color: '#409EFF',
                           }}>{'保存'}</Text>
                       </TouchableOpacity>
                       <TouchableOpacity
                           onPress={this.cancelPress} style={{
                           height: pxToDp(40),
                           width: pxToDp(100),
                           marginLeft: '14%',
                           flexDirection: 'row',
                           alignItems: 'center',
                           display: this.state.isEditting?'flex':'none',
                       }}>
                           <Image source={require('./pic/points.png')} style={{
                               marginLeft: '20%',
                               height: pxToDp(20),
                               width: pxToDp(20),
                           }}/>
                           <Text style={{
                               color: '#409EFF',
                           }}>{'取消'}</Text>
                       </TouchableOpacity>
                   </View>
                   <View style={{
                       height: '5%',
                   }}></View>
               </View>
           </View>
       )
   }
};


调用:个人资料页面


import React from 'react';
import {StyleSheet, Text, View, Dimensions, Image, Button} from 'react-native';
import PeopleInformation from './PeopleInformation';

const App = () => {
 return (
     <View>
         <PeopleInformation/>
     </View>
 );
};

export default App;


在这里插入图片描述在这里插入图片描述在这里插入图片描述

注意

该文章仅个人学习使用,欢迎大家一起交流学习

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宇智波盆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值