本文主要包括以下内容

  1. View组件的实例
  2. Text组件实例
  3. Navigator组件实例
  4. TextInput组件实例

View组件的实例

效果如下

代码如下

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. PixelRatio,
  12. View
  13. } from 'react-native';
  14. class Abc extends Component {
  15. render() {
  16. return (
  17. <View style={styles.flex}>
  18. <View style={styles.container}>
  19. <View style={[styles.item,styles.center]}>
  20. <Text style={styles.font}>酒店</Text>
  21. </View>
  22. <View style={[styles.item,styles.lineLeftRight]}>
  23. <View style={[styles.center,styles.flex,styles.lineCenter]}>
  24. <Text style={styles.font}>海外酒店</Text>
  25. </View>
  26. <View style={[styles.center,styles.flex]}>
  27. <Text style={styles.font}>特惠酒店</Text>
  28. </View>
  29. </View>
  30. <View style={styles.item}>
  31. <View style={[styles.center,styles.flex,styles.lineCenter]}>
  32. <Text style={styles.font}>团购</Text>
  33. </View>
  34. <View style={[styles.center,styles.flex]}>
  35. <Text style={styles.font}>客栈,公寓</Text>
  36. </View>
  37. </View>
  38. </View>
  39. </View>
  40. );
  41. }
  42. }
  43. const styles = StyleSheet.create({
  44. container: {
  45. marginTop:200,
  46. marginLeft:5,
  47. marginRight:5,
  48. height:84,
  49. flexDirection:'row',
  50. borderRadius:5,
  51. padding:2,
  52. backgroundColor:'#FF0067',
  53. },
  54. item: {
  55. flex: 1,
  56. height:80,
  57. },
  58. center:{
  59. justifyContent:'center',
  60. alignItems:'center',
  61. },
  62. flex:{
  63. flex:1,
  64. },
  65. font:{
  66. color:'#fff',
  67. fontSize:16,
  68. fontWeight:'bold',
  69. },
  70. lineLeftRight:{
  71. borderLeftWidth:1/PixelRatio.get(),
  72. borderRightWidth:1/PixelRatio.get(),
  73. borderColor:'#fff',
  74. },
  75. lineCenter:{
  76. borderBottomWidth:1/PixelRatio.get(),
  77. borderColor:'#fff',
  78. },
  79. });
  80. AppRegistry.registerComponent('Abc', () => Abc);

Text组件实例

head.js

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. PixelRatio,
  12. View
  13. } from 'react-native';
  14. class Header extends Component {
  15. render() {
  16. return (
  17. <View style={styles.flex}>
  18. <Text style={styles.font}>
  19. <Text style={styles.font_1}>网易</Text>
  20. <Text style={styles.font_2}>新闻</Text>
  21. <Text>有态度"</Text>
  22. </Text>
  23. </View>
  24. );
  25. }
  26. }
  27. const styles = StyleSheet.create({
  28. flex:{
  29. marginTop:25,
  30. height:50,
  31. borderBottomWidth:3/PixelRatio.get(),
  32. borderBottomColor:'#EF2D36',
  33. alignItems:'center',
  34. },
  35. font:{
  36. fontSize:25,
  37. fontWeight:'bold',
  38. textAlign:'center',
  39. },
  40. font_1:{
  41. color:'#CD1D1C',
  42. },
  43. font_2:{
  44. color:'#FFF',
  45. backgroundColor:'#CD1D1C',
  46. },
  47. });
  48. module.exports=Header;

将head.js导入到主JS中的代码为const Header=require(‘./head’);

主JS详细代码

实现了列表,并且有点击效果

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Text,
  11. PixelRatio,
  12. View
  13. } from 'react-native';
  14. const Header=require('./head');
  15. class Abc extends Component {
  16. render() {
  17. return (
  18. <View style={styles.flex}>
  19. <Header></Header>
  20. <List title='一线城市楼市退烧 有房源一夜跌价160万'></List>
  21. <List title='上海市民称墓地太贵买不起 买房存骨灰'></List>
  22. <List title='朝鲜再发视频:摧毁青瓦台 一切化作灰烬'></List>
  23. <List title='生活大爆炸人物原型都好牛逼'></List>
  24. <ImportantNews
  25. news={[
  26. '解放军报报社大楼正在拆除 标识已被卸下(图)',
  27. '韩国停签东三省52家旅行社 或为阻止朝旅游创汇',
  28. '南京大学生发起亲吻陌生人活动 有女生献初吻-南京大学生发起亲吻陌生人活动 有女生献初吻-南京大学生发起亲吻陌生人活动 有女生献初吻',
  29. '防总部署长江防汛:以防御98年量级大洪水为目标'
  30. ]}>
  31. </ImportantNews>
  32. </View>
  33. );
  34. }
  35. }
  36. class List extends Component{
  37. render(){
  38. return (
  39. <View style={styles.list_item}>
  40. <Text style={styles.list_item_font}>{this.props.title}</Text>
  41. </View>
  42. );
  43. }
  44. }
  45. class ImportantNews extends Component{
  46. show(title){
  47. alert(title);
  48. }
  49. render(){
  50. var news=[];
  51. for(var i in this.props.news){
  52. var text=(
  53. <Text
  54. onPress={this.show.bind(this,this.props.news[i])}
  55. numberOfLines={2}
  56. style={styles.news_item}
  57. key={i}
  58. >{this.props.news[i]}</Text>
  59. );
  60. news.push(text);
  61. }
  62. return (
  63. <View style={styles.flex}>
  64. <Text style={styles.news_title}>今日要闻</Text>
  65. {news}
  66. </View>
  67. );
  68. }
  69. }
  70. const styles = StyleSheet.create({
  71. flex:{
  72. flex:1,
  73. },
  74. list_item:{
  75. height:40,
  76. marginLeft:10,
  77. marginRight:10,
  78. borderBottomWidth:1,
  79. borderBottomColor:'#ddd',
  80. justifyContent:'center',
  81. },
  82. list_item_font:{
  83. fontSize:16,
  84. },
  85. news_item:{
  86. marginLeft:10,
  87. marginRight:10,
  88. fontSize:15,
  89. lineHeight:40,
  90. },
  91. news_title:{
  92. fontSize:20,
  93. fontWeight:'bold',
  94. color:'#CD1D1C',
  95. marginLeft:10,
  96. marginTop:15,
  97. },
  98. });
  99. AppRegistry.registerComponent('Abc', () => Abc);

效果如下

Navigator组件实例

实现了页面跳转和通过Navigator传递数据并回传数据,在componentDidMount中获取传递过来的数据

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Navigator,
  11. ScrollView,
  12. Text,
  13. PixelRatio,
  14. View
  15. } from 'react-native';
  16. const Header=require('./head');
  17. class Abc extends Component {
  18. render() {
  19. let defaultName='List';
  20. let defaultComponent=List;
  21. return (
  22. <Navigator
  23. initialRoute={{ name: defaultName, component: defaultComponent }}
  24. //配置场景
  25. configureScene=
  26. {
  27. (route) => {
  28. //这个是页面之间跳转时候的动画,具体有哪些?可以看这个目录下,有源代码的: node_modules/react-//native/Libraries/CustomComponents/Navigator/NavigatorSceneConfigs.js
  29. return Navigator.SceneConfigs.VerticalDownSwipeJump;
  30. }
  31. }
  32. renderScene={
  33. (route, navigator) =>
  34. {
  35. let Component = route.component;
  36. return <Component {...route.params} navigator={navigator} />
  37. }
  38. } />
  39. ); }
  40. }
  41. class List extends Component {
  42. constructor(props) {
  43. super(props);
  44. this.state = {
  45. id:1,
  46. user:null,
  47. };
  48. }
  49. _pressButton() {
  50. const { navigator } = this.props;
  51. //为什么这里可以取得 props.navigator?请看上文:
  52. //<Component {...route.params} navigator={navigator} />
  53. //这里传递了navigator作为props
  54. const self=this;
  55. if(navigator) {
  56. navigator.push({
  57. name: 'Detail',
  58. component: Detail,
  59. params:{
  60. id:this.state.id,
  61. //从详情页获取user
  62. getUser: function(user) {
  63. self.setState({
  64. user: user
  65. })
  66. }
  67. }
  68. })
  69. }
  70. }
  71. render(){
  72. if(this.state.user){
  73. return(
  74. <View>
  75. <Text style={styles.list_item}>用户信息: { JSON.stringify(this.state.user) }</Text>
  76. </View>
  77. );
  78. }else{
  79. return (
  80. <ScrollView style={styles.flex}>
  81. <Text style={styles.list_item} onPress={this._pressButton.bind(this)} >☆ 豪华邮轮济州岛3日游</Text>
  82. <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>☆ 豪华邮轮台湾3日游</Text>
  83. <Text style={styles.list_item} onPress={this._pressButton.bind(this)}>☆ 豪华邮轮地中海8日游</Text>
  84. </ScrollView>
  85. );
  86. }
  87. }
  88. }
  89. const USER_MODELS = {
  90. 1: { name: 'mot', age: 23 },
  91. 2: { name: '晴明大大', age: 25 }
  92. };
  93. class Detail extends Component{
  94. constructor(props) {
  95. super(props);
  96. this.state = {
  97. id:null
  98. };
  99. }
  100. componentDidMount() {
  101. //这里获取从FirstPageComponent传递过来的参数: id
  102. this.setState({
  103. id: this.props.id
  104. });
  105. }
  106. _pressButton() {
  107. const { navigator } = this.props;
  108. if(this.props.getUser) {
  109. let user = USER_MODELS[this.props.id];
  110. this.props.getUser(user);
  111. }
  112. if(navigator) {
  113. //很熟悉吧,入栈出栈~ 把当前的页面pop掉,这里就返回到了上一个页面:List了
  114. navigator.pop();
  115. }
  116. }
  117. render(){
  118. return(
  119. <ScrollView>
  120. <Text style={styles.list_item} >传递过来的用户id是:{this.state.id}</Text>
  121. <Text style={styles.list_item} onPress={this._pressButton.bind(this)} >点击我可以跳回去</Text>
  122. </ScrollView>
  123. );
  124. }
  125. }
  126. const styles = StyleSheet.create({
  127. flex:{
  128. flex:1,
  129. },
  130. list_item:{
  131. height:40,
  132. marginLeft:10,
  133. marginRight:10,
  134. fontSize:20,
  135. borderBottomWidth:1,
  136. borderBottomColor:'#ddd',
  137. justifyContent:'center',
  138. },
  139. });
  140. AppRegistry.registerComponent('Abc', () => Abc);

效果如下

TextInput组件实例

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import React, { Component } from 'react';
  7. import {
  8. AppRegistry,
  9. StyleSheet,
  10. Navigator,
  11. ScrollView,
  12. TextInput,
  13. Text,
  14. PixelRatio,
  15. View
  16. } from 'react-native';
  17. class Abc extends Component {
  18. render() {
  19. return (
  20. <View style={[styles.flex,styles.topStatus]}>
  21. <Search></Search>
  22. </View>
  23. );
  24. }
  25. }
  26. class Search extends Component {
  27. render(){
  28. return(
  29. <View style={[styles.flex,styles.flexDirection]}>
  30. <View style={[styles.flex,styles.input]}>
  31. <TextInput returnKeyType="search" />
  32. </View>
  33. <View style={styles.btn}>
  34. <Text style={styles.search}>搜索</Text>
  35. </View>
  36. </View>
  37. );
  38. }
  39. }
  40. const styles = StyleSheet.create({
  41. flex:{
  42. flex:1,
  43. },
  44. flexDirection:{
  45. flexDirection:'row',
  46. },
  47. topStatus:{
  48. marginTop:25,
  49. },
  50. input:{
  51. height:45,
  52. borderColor:'red',
  53. borderWidth:1,
  54. marginLeft:10,
  55. paddingLeft:10,
  56. borderRadius:5,
  57. },
  58. btn:{
  59. width:45,
  60. marginLeft:-5,
  61. marginRight:5,
  62. backgroundColor:'#23BEFF',
  63. height:45,
  64. justifyContent:'center',
  65. alignItems:'center',
  66. },
  67. search:{
  68. color:'#fff',
  69. fontSize:15,
  70. fontWeight:'bold',
  71. },
  72. });
  73. AppRegistry.registerComponent('Abc', () => Abc);

效果如下

React Native实例的更多相关文章

  1. React Native实例之房产搜索APP

    React Native 开发越来越火了,web app也是未来的潮流, 现在react native已经可以完成一些最基本的功能. 通过开发一些简单的应用, 可以更加熟练的掌握 RN 的知识. 在学 ...

  2. React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例)

    React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例) TextInput组件介绍 TextInput是一个允许用户在应用中通过键盘输入文本的基本组 ...

  3. React Native之FlatList的介绍与使用实例

    React Native之FlatList的介绍与使用实例 功能简介 FlatList高性能的简单列表组件,支持下面这些常用的功能: 完全跨平台. 支持水平布局模式. 行组件显示或隐藏时可配置回调事件 ...

  4. 超具体Windows版本号编译执行React Native官方实例UIExplorer项目(多图慎入)

    ),React Native技术交流4群(458982758).请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客右側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...

  5. 【React Native开发】React Native For Android环境配置以及第一个实例(1)

    年9月15日也公布了ReactNative for Android,尽管Android版本号的项目公布比較迟,可是也没有阻挡了广大开发人员的热情.能够这样讲在2015年移动平台市场上有两个方向技术研究 ...

  6. 【React Native开发】React Native控件之ListView组件解说以及最齐全实例(19)

    ),React Native技术交流4群(458982758).请不要反复加群!欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章 ...

  7. 【React Native开发】React Native控件之Image组件解说与美团首页顶部效果实例(10)

    ),React Native技术交流4群(458982758),欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送! Im ...

  8. React Native ——入门环境搭配以及简单实例

    一.Homebrew 是OS X 的套件管理器. 首先我们需要获取 Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent. ...

  9. React Native:使用 JavaScript 构建原生应用

    [转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...

随机推荐

  1. iOS 图片 的 聊天气泡显示 Objective-C

    - (void)viewDidLoad { [super viewDidLoad]; UIImageView *ImageView01 = [[UIImageView alloc] init]; [I ...

  2. Xcode 中的黄色文件夹/蓝色文件夹

    蓝色.黄色首先是和你导入文件夹时的勾选项目有关系: 黄色:-->group 蓝色:--> folder 在group中的.m/.h文件,#import "xxxxx.h" ...

  3. VirtualBox CentOS安装增强功能与设置共享文件夹

    如果安装的是CentOS minimal版无网络的可以看这篇文章. 一.安装依赖环境 依次执行如下命令 yum install update yum install kernel-headers yu ...

  4. Opencv角点检测

    #include "stdafx.h" #define max_corners 20 int main() { int cornerNum = max_corners; vecto ...

  5. Python自动化之sqlalchemy关联查询

    外键关联 from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship class Address(Base): ...

  6. COGS 902 乐曲主题 题解 & hash入门贺

    [题意] 给定一个长为n的序列,元素都是不超过88的正整数,求序列中主题的最大长度. 所谓主题是指在序列中出现了至少两次并且不相交的子串.特别的,主题可以变调,也就是说如果一个子串全部加上或减去一个数 ...

  7. Jquery ajax调用webservice总结

    jquery ajax调用webservice(C#)要注意的几个事项: 1.web.config里需要配置2个地方 <httpHandlers>      <remove verb ...

  8. poj 1220(短除法)

    http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...

  9. poj 1700

    http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { ...

  10. Java从网络读取图片并保存至本地

    package cn.test.net; import java.io.File; import java.io.FileOutputStream; import java.io.InputStrea ...