React-native第一课,Button的添加
来源:互联网 发布:淘宝介入卖家退货拒收 编辑:程序博客网 时间:2024/06/09 01:13
前两天开始着手react-native,之前的时间都费在环境上了,今天正式代码的第一课。看过网上的资料,现在综合整理一下以备后续查看。
首先,react-native 的环境配置见http://reactnative.cn/docs/0.24/getting-started.html#content
然后进入代码环节了,我这里编写代码的环境是Atom。
首先是在页面中显示一个Button,图片如下。
实现代码如下,修改index.android文件,我这里用到的按钮控件为 TouchableOpacity
import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
TouchableOpacity <--加入引用
} from 'react-native';
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button}> 加入控件,这里的属性设置为使用的style,以下会有代码
<Text style={styles.textstyle}>
确定 <--------显示字体
</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({ <-------style定义
button:{
height: 80,
width: 200,
backgroundColor:'gray',
justifyContent:'center',
borderRadius: 20,
},
textstyle:{
textAlign:'center',
fontSize:20,
},......
这样单纯的显示Button就完成了,其次关心的一定是Button的点击事件了,那么以下我们就来实现以下Button的点击事件吧
给 TouchableOpacity添加点击事件的属性是onPress,有点像android中在xml给button添加点击事件的方法。
class Test extends Component {
onclick=() => {
alert('我点击了按钮');
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
onPress ={this.onclick}
>
<Text style={styles.textstyle}>
确定
</Text>
</TouchableOpacity>
</View>
);
}
}
这样的话简单的点击事件就处理完了。我们下面尝试自己封装一个Button,主要牵扯到一些JS的用法,对于像我这种java选手刚接触到JS的来说,下面的代码还是蛮精致的。
首先沾代码的。自己的Button
/**
* xiaoaxue 2016.4.21
*/
import React, {
Component,
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
export default class Button extends Component {
constructor(props){
super(props);
this.state = {
_text:props.text,
}
}
onclick=() => {
alert('我点击了按钮');
};
render() {
//大括号叫解构,将props中的属性text取出来放入text中,好处是可以取多个值
const {text,background} = this.props;
return (
<View style={styles.container}>
<TouchableOpacity
style={[styles.button,{backgroundColor:background}]}>
<Text style={styles.textstyle}>
{/*{this.props.text}*/}
{this.state._text}
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
button:{
height: 80,
width: 200,
backgroundColor:'gray',
justifyContent:'center',
borderRadius: 20,
},
textstyle:{
textAlign:'center',
fontSize:20,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
index中修改:
import Button from './src/component/Button';
class Test extends Component {
render() {
return (
<View style={styles.container}>
{/* props,属性*/}
<Button text = "确定" background ="red"/>
<Button text = "取消" background ="gray"/>
</View>
);
}
}
代码要点:
1. 自己封装Button时通过构造方法传参数取值有两种方式
①: 构造参数为props,在构造方法内部取值
this.state = {
_text:props.text,
}
调用时为this.state._text
②: 解构
const {text,background} = this.props;
优点在于可解多个,调用时为this.props.text
- React-native第一课,Button的添加
- React Native第一课
- React Native 封装自定义的Button
- React Native Button实现
- react-native Button
- React Native Button使用
- react native button 按钮
- React-Native(三)--Button
- React Native自定义Button
- 我的第一个React Native App
- React Native 的第一个Android 项目
- React Native ios开发第一课
- React-native简单button实现
- react-native-Button-TouchableOpacity
- react-native--万能Button封装
- react native Button 使用详解
- React Native学习七- button
- React Native组件之Button
- leetcode 4. Median of Two Sorted Arrays
- C语言基础函数 已知摄氏度求华氏度
- 一点点
- jQuery EasyUI 表单
- don't run elasticsearch as root.
- React-native第一课,Button的添加
- HTML + CSS 之替换元素与非替换元素
- 《编写高质量代码 : 改善C#程序的157个建议》读书笔记 1-10
- HDU 1565 1569 方格取数 (最小割)
- 最终实现任意个数字排序
- 配置文件——App.config文件读取和修改
- 教你了解数码相机的种类与区别
- ScrollView嵌套EditText,导致EditText显示多行时滑动失效的问题
- 两位数乘一位数