5 - React Native从入门到入土 简单实现qq登录页
2017-07-23 本文已影响261人
翻滚的炒勺2013

目标:理解React Native面向组件开发
0.桌面新建一个文件夹,打开终端创建一个新工程并运行
react-native init 你工程你的名字
cd 你工程的名字
react-native run-ios


这个时候模拟器已经运行起来了

1.用webStorm打开工程,等待读条

需要点击switch转换成webStorm可以识别的语言


先来看这个界面,如果在iOS中就是UIImageView,UITextFiled,UIButton,UILabel组成的页面,在react-native中也是差不多的下面是他们的对应关系
|oc| react - native |
| :---- | :----- | :----|:----- |
| UIImageView |Image |
| UITextFiled |TextInput |
| UILabel |Text |
UIButton以后再说
知道要了用哪些控件在react-native中需要导入这些控件

代码说明:
<控件的类名(我这样理解的) tyle={styles.loginTextStyle}(设置它是什么样式的) />
Image获取本地图片<Image source={require('./img/icon.png')}/> 图片的目录结构如图:
5094884F-FE3E-4903-843D-47F11CEBC4D1.png
获取当前屏幕的宽度
var Dimensions = require("Dimensions");
var width = Dimensions.get('window').width;
在其他的地方直接调用width即可
代码部分
export default class learnRN extends Component {
render() {
return (
<View style={styles.container}>
/// 创建一个头像Image,样式为iconnStyle}
<Image source={require('./img/icon.png')} style={styles.iconnStyle} />
/// 创建输入框,样式为.textInputStyle, placeholder是占位文字
<TextInput placeholder={"请输入用户名"} style={styles.textInputStyle} />
<TextInput placeholder={"请输入密码"} password={true} style={styles.textInputStyle} />
<View style={styles.loginStyle}>
<Text style={styles.loginTextStyle}> 登录</Text>
</View>
<View style={styles.settingStyle}>
<Text style={styles.settingTextStyle}>无法登录</Text>
<Text style={styles.settingTextStyle}>新用户</Text>
</View>
<View style={styles.otherStyle}>
<Text>其他方式登录</Text>
<Image source={require('./img/icon3.png')} style={styles.otherImageStyle}/>
<Image source={require('./img/icon7.png')} style={styles.otherImageStyle}/>
<Image source={require('./img/icon8.png')} style={styles.otherImageStyle}/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
otherImageStyle: {
height:50,
width: 50,
borderRadius:25,
marginLeft:10
},
otherStyle: {
/// 将侧轴设为横向
flexDirection:'row',
/// 竖向中心对齐
alignItems:'center',
/// 设置绝对位置
position:'absolute',
bottom:10,
left:10,
},
settingTextStyle: {
color:'black'
},
settingStyle: {
/// 将侧轴设为横向
flexDirection:'row',
width: width,
/// 设置为;两端对齐
justifyContent:'space-between'
},
loginTextStyle: {
color:'white',
},
loginStyle:{
height:35,
width: width * 0.8,
backgroundColor: 'blue',
justifyContent:'center',
alignItems:'center',
marginTop:30,
marginBottom: 10,
/// 设置圆角
borderRadius:5,
},
container: {
flex: 1,
alignItems: 'center',
backgroundColor: '#dddddd',
},
iconnStyle: {
marginTop:50,
marginBottom: 30,
width:80,
height:80,
borderRadius:40,
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
textInputStyle:{
height:38,
width: width,
marginBottom: 1,
backgroundColor: 'white',
textAlign:'center'
},
});
组件开发
新创建一个loginView的js文件
将index.ios.js中所有代码赋值到loginView.js
将最后的代码
AppRegistry.registerComponent('learnRN', () => learnRN);
改成如下形式: module.exports和oc中return作用差不多返回这个类
module.exports = loginView;
同时修改类名为loginView

在index.ios.js中引入loginView这类
var LoginView = require('./loginView');
在主函数中实例化这个类
export default class learnRN extends Component {
render() {
return (
<LoginView />
)
}
}