1 触摸事件

普通点击我们可以使用onPress方法,我们可以使用Touchable 系列控件设计我们的按钮

TouchableHighlight 背景会在用户手指按下时变暗

TouchableNativeFeedback用户手指按下时形成类似墨水涟漪的视觉效果

TouchableOpacity指按下时降低按钮的透明度,而不会改变背景的颜色

TouchableWithoutFeedback 不显示任何视觉反馈

检测用户是否进行了长按操作,可以在上面列出的任意组件中使用onLongPress属性来实现

2 测试关键代码如下

要记得导入相应的组件

import React, {Component} from 'react';
import {Platform, ScrollView, StyleSheet, Text, View, TextInput, NativeModules, DeviceEventEmitter, Image, Button, AppRegistry, TouchableHighlight, TouchableOpacity, TouchableNativeFeedback, TouchableWithoutFeedback} from 'react-native';
export default class App extends Component<Props> {render() {return (<View style={styles.container}><View style={styles.buttonContainer}><Button onPress={this.showMsg} title="press me"/></View>  <View style={styles.buttonContainer}><Button onPress={this.showMsg} title="press me" color="#841584"/></View> <View style={styles.buttonContainer}><TouchableHighlight onPress={this.showMsg} underlayColor="white"><View style={styles.button}><Text style={styles.text}>TouchableHighlight</Text></View></TouchableHighlight></View> <View style={styles.buttonContainer}><TouchableOpacity onPress={this.showMsg}><View style={styles.button}><Text style={styles.text}>TouchableOpacity</Text></View></TouchableOpacity></View> <View style={styles.buttonContainer}><TouchableNativeFeedback onPress={this.showMsg}><View style={styles.button}><Text style={styles.text}>TouchableNativeFeedback</Text></View></TouchableNativeFeedback></View> <View style={styles.buttonContainer}><TouchableWithoutFeedback onPress={this.showMsg}><View style={styles.button}><Text style={styles.text}>TouchableWithoutFeedback</Text></View></TouchableWithoutFeedback></View> <View style={styles.buttonContainer}><TouchableWithoutFeedback onLongPress={this.showMsg}><View style={styles.button}><Text style={styles.text}>onLongPress me</Text></View></TouchableWithoutFeedback></View> <View style={styles.layoutButtonContainer}><Button onPress={this.showMsg} title="onLongPress me"/><Button onPress={this.showMsg} title="onLongPress me" color="#841584"/></View> </View>);}//记得这里调用的时候不需要加上()showMsg(){alert("showMsg(){}");  }//记得末尾加上分号,调用的时候也要加上()showMessage = () => {alert("showMessage = () => {}");};
}const styles = StyleSheet.create({container: {flex: 1,justifyContent: 'center'},buttonContainer: {margin:10},layoutButtonContainer: {margin:10,flexDirection: 'row',justifyContent: 'space-between'},button: {alignItems: 'center',backgroundColor: '#842534'},text: {padding: 10,color: 'white'}
});

3 手机效果如下

React Native之触摸事件(Touchable系列和onLongPress)相关推荐

  1. React Native 手势触摸事件机制详解(进阶篇)

    源码已开源到Github,详细代码可以查看:<React Native 触摸事件代码实践>. 在基础篇,对RN中的触摸事件做了详细的介绍.相信大家对于触摸事件流程机制有了更为清晰的认识.没 ...

  2. React Native按钮详解|Touchable系列组件使用详解

    转载自:http://www.devio.org/2017/01/10/React-Native按钮详解-Touchable系列组件使用详解/ 在做App开发过程中离不了的需要用户交互,说到交互,我们 ...

  3. React Native 手势触摸事件机制详解(基础篇)

          欢迎大家关注[跨平台开发那些事]公众号,定期推送跨平台开发技术实践.        源码已开源到Github,详细代码可以查看:<React Native 触摸事件代码实践>. ...

  4. react native手势触摸

    简介 在诸多移动应用中,需要有许多手势识别的使用场景.比如当涂抹.上滑.下滑.侧滑.拖动等,都涉及到方方面面的功能交互.而在react native中,就有提供处理这些手势的API,下面就来一一介绍相 ...

  5. React Native - 3 View, Text简介以及onPress onLongPress事件

    我们要生成如下的构图 直接上图,不解释. 如下图所示,定义函数,函数之间不需要逗号,在元素上添加事件,使用关键字this.{function name} 转载于:https://www.cnblogs ...

  6. 【转】【React Native开发】

    [React Native开发]React Native控件之ListView组件讲解以及最齐全实例(19)  [React Native开发]React Native控件之Touchable*系列组 ...

  7. React Native专题

    未经授权不得转载: 出处地址:http://www.lcode.org 本文出自:[江清清的技术专栏] 本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入 ...

  8. React Native专题-江清清

    本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶相关讲解. 刚创建的React Native交流8群:533435865  欢迎各位大牛, ...

  9. React Native专题-江

    (一).基本介绍: 江博客http://blog.csdn.net/jiangqq781931404/article/category/6055594 React Native For Android ...

最新文章

  1. Android 适配底部返回键等虚拟键盘的完美解决方案
  2. 判断一个点是否在三角形内部
  3. 网易云信+妙克:从零到标杆的在线音视频音乐教学之路
  4. 前端学习(509):水平和垂直居中第二种方式
  5. Convert Sorted Array to Binary Search Tree With Minimal Height
  6. 时间操作(Java版)—获取距离系统时间N天后的日期时间信息
  7. 项目管理常用文档表格模板一
  8. log2n怎么算计算机公式,log函数运算公式是什么
  9. 请求头显示Provisional headers are shown问题
  10. Java 小白 声明两个字符串:一个是“宋江,卢俊义,林冲,鲁智深,武松“;另一个是“及时雨,玉麒麟,豹子头,花和尚,行者“。以逗号为分隔符分割两个字符串,然后将人物绰号和名字拼接在一起并输出。
  11. ES2015 模板字符串与字符串扩展方法
  12. 【中亦安图】导致Oracle性能抖动的参数提醒(4)
  13. JS:对象的浅克隆、深克隆
  14. 局域网内抢网速_为什么4G的网速越来越慢?
  15. 堆积木(vector数组)
  16. oracle9i如何建表,oracle9i命令(一):手工创建数据库
  17. 《Android4游戏编程入门经典》读后感
  18. 代码修炼Day1_LeetCode704二分查找27移除元素
  19. 关于Android单元测试
  20. WebSocket 协议讲解

热门文章

  1. 拥抱开源!除了微软红帽,这些国际大厂你认识几个?
  2. Magicodes.IE之快速导出Excel
  3. ASP.NET Core 6 Minimal API
  4. 你怕是对MD5算法有误解
  5. 云原生 | .NET 5 with Dapr 初体验
  6. 反射 + 抽象工厂模式切换不同的实现方法
  7. Delphi 26 岁
  8. C# 9 新特性:代码生成器、编译时反射
  9. 自制 .NET Core 路由调试中间件
  10. 多角度让你彻底明白yield语法糖的用法和原理及在C#函数式编程中的作用