Touchable系列组件
来源:互联网 发布:金融界名人名言 知乎 编辑:程序博客网 时间:2024/06/05 07:19
学习资料
博客:Touchable系列组件
TouchableHighlight、TouchableOpacity
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, TouchableOpacity} from 'react-native';class TouchableDemo extends Component { render() { return ( <View style={styles.container}> <TouchableOpacity activeOpacity={0.5}> <View style={styles.innerViewStyle}> <Text>我是文本,但可点击</Text> </View> </TouchableOpacity> </View> ); }}const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, innerViewStyle: { backgroundColor: 'red', }});AppRegistry.registerComponent('TouchableDemo', () => TouchableDemo);
不透明触摸 TouchableOpacity
该组件封装了响应触摸事件;当点击按下的时候,该组件的透明度会降低。
常用属性:activeOpacity 设置当用户触摸的时候,组件的透明度
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, TouchableOpacity, AlertIOS} from 'react-native';// ES5新建组件的写法var TouchableDemo = React.createClass({ getInitialState(){ return { title:'不透明触摸' } }, render() { return ( <View style={styles.container}> <TouchableOpacity activeOpacity={0.5} onPress={()=>this.activeEvent('点击')} onPressIn={()=>this.activeEvent('按下')} onPressOut={()=>this.activeEvent('抬起')} onLongPress={()=>this.activeEvent('长按')} > <View style={styles.innerViewStyle}> <Text>点击事件</Text> </View> </TouchableOpacity> <View style={{marginTop:20}}> <Text>状态: {this.state.title}</Text> </View> </View> ); }, // 鼠标触发这个函数 activeEvent(event){ // 修改title this.setState({ title:event }) }});// ES6的写法// class TouchableDemo extends Component {//// }const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, innerViewStyle: { backgroundColor: 'red', }});AppRegistry.registerComponent('TouchableDemo', () => TouchableDemo);
补充:
上面我们用的是ES5的写法,下面我们来用ES6的写法实现:
// ES6的写法class TouchableDemo extends Component { // 构造 constructor(props) { super(props); // 初始状态 this.state = {title:'不透明触摸'}; } render() { return ( <View style={styles.container}> <TouchableOpacity activeOpacity={0.5} onPress={()=>this.activeEvent('点击')} onPressIn={()=>this.activeEvent('按下')} onPressOut={()=>this.activeEvent('抬起')} onLongPress={()=>this.activeEvent('长按')} > <View style={styles.innerViewStyle}> <Text>点击事件</Text> </View> </TouchableOpacity> <View style={{marginTop:20}}> <Text>状态: {this.state.title}</Text> </View> </View> ); } // 鼠标触发这个函数 activeEvent(event){ // 修改title this.setState({ title:event }) }}
对比2种写法的区别。
这里边还需要学习 React Native组件生命周期
0 0
- Touchable系列组件
- React Native Touchable系列组件
- react native Touchable 系列组件使用详解
- React Native组件篇(四) — Touchable系列组件
- React Native控件之Touchable*系列组件详解(28)
- React Native按钮详解|Touchable系列组件使用详解
- React Native按钮详解|Touchable系列组件使用详解
- ReactNative Touchable 组件
- TextInput组件和Touchable组件
- (五)React Native---Touchable组件
- react native 封装Touchable 组件
- 【React Native开发】React Native控件之Touchable*系列组件详解(18)
- 一起来点React Native——常用组件之Touchable系列
- 【React Native开发】React Native控件之Touchable*系列组件详解(18)
- 【REACT NATIVE 系列教程之一】触摸事件的两种形式与四种TOUCHABLE组件详解
- React Native交互组件之Touchable
- React Native——Touchable类组件
- React Native 组件之Touchable*源码解析
- Python+Selenium实现微博自动化爬虫
- 剑指offer(动态规划-LeetCode72)
- 如何写出高效C++(构造/析构/赋值运算)
- BOM操作
- 03.运算符
- Touchable系列组件
- 【代码笔记】iOS-字体抖动动画
- sersync2实时同步
- 在win7(32位)系统上装ubuntu(64位)虚拟机,提示二进制长模式不兼容
- CSharp进行JSON的序列化和反序列化
- POJ 2508
- Google推荐的图片加载库Glide使用
- - Wooden Sticks
- Android禁止ViewPager的左右滑动