React Native Knowledge 5-Touchable Class components, react5-touchable
React Native does not bind a click event to the element as the web does. We already know that the Text component has an onPress event.
Also bound to click events, React Native provides three components to do this.
1. TouchableHighlight: highlighted touch. When you click it, it will produce a highlighted effect.
2. TouchableOpacity: transparent touch. When a user clicks, the clicked component is transparent.
3. TouchableWithoutFeedback: no feedback touch. The user clicks without any visual effect.
Note: Only one child node is supported. If you want to include multiple child components, use one View to wrap them.
I. TouchableHighlight
1: When you press your finger, the opacity of the View is reduced and the corresponding color is displayed. The implementation principle is to add a new View at the underlying layer. In addition, TouchableHighlight can only be nested at one layer, not at multiple layers.
Common attributes:
1.1: activeOpacity number
Set the opacity of the widget when it is touched (value range: 0-1)
1.2: onHideUnderlay function Method
Called when the underlying layer is hidden
1.3: onShowUnderlay function Method
Called when the underlying layer is displayed
1.4: style
You can set the style demonstration of the control. For details about the style demonstration, refer to the style of the View component.
1.5: underlayColor
The color displayed when you touch or click the control.
2: instance code
Import React, {Component} from 'react '; import {AppRegistry, StyleSheet, Text, View, TextInput, Alert, Image, TouchableHighlight} from 'react-native '; class ReactNativeProject extends Component {_ show (text) {alert (text);} render () {return (<View style = {styles. container }> <TouchableHighlight onPress = {this. _ show. bind (this, 'react Native ')} underlayColor =' # E1F6FF '> <Text style = {styles. item}> React Native </Text> </TouchableHighlight> <TouchableHighlight onPress = {this. _ show. bind (this, 'click effect')} underlayColor = '# E1F6FF'> <Text style = {styles. item }> click effect </Text> </TouchableHighlight> </View>) ;}} const styles = StyleSheet. create ({container: {flex: 1, marginTop: 64}, item: {fontSize: 18, marginLeft: 5, color: '#434343'}); AppRegistry. registerComponent ('reactnativeproject', () => ReactNativeProject );
:
2. opaque touch TouchableOpacity
1: this component encapsulates the response to the touch event. When you click and press it, the transparency of this component decreases. This component does not need to set the background color, which makes it easier to use;
Common attributes:
ActiveOpacity number
Sets the transparency of components when users touch them.
2: instance code
Import React, {Component} from 'react '; import {AppRegistry, StyleSheet, Text, View, TextInput, Alert, Image, TouchableHighlight, TouchableOpacity} from 'react-native '; class ReactNativeProject extends Component {_ show (text) {alert (text);} render () {return (<View style = {styles. container }> <TouchableOpacity onPress = {this. _ show. bind (this, 'react Native ')} activeOpacity = {0.5}> <Text style = {styles. item}> React Native </Text> </TouchableOpacity> <TouchableOpacity onPress = {this. _ show. bind (this, 'click effect')} activeOpacity = {0.9}> <Text style = {styles. item }> click effect </Text> </TouchableOpacity> </View>); }} const styles = StyleSheet. create ({container: {flex: 1, marginTop: 64}, item: {fontSize: 18, marginLeft: 5, color: '#434343'}); AppRegistry. registerComponent ('reactnativeproject', () => ReactNativeProject );
:
3. TouchableWithoutFeedback
Do not use this component unless you have a good reason. All elements that can respond to touch screen operations should have a visual feedback after the touch screen (however, this component does not have any visual feedback ). This is one of the main reasons why a "web" application is always "native. TouchableWithoutFeedback only supports one child node. If you want to include multiple child components, use one View to wrap them.
4. knowledge points
1: Two Methods of react native parameter bind this (if bind is not used, the event will be executed immediately upon loading)
The first method with the bind this parameter (use the arrow method)
<TouchableOpacity onPress={(e) => this._needHandlderArgument(e,argument)} underlayColor='#00EE76'> </TouchableOpacity >
_ NeedHandlderArgument (e, argument) {// only process argument e for binding this}
The second method with the bind this parameter (directly with the bind parameter)
<TouchableOpacity onPress={this._needHandlderArgument.bind(this,argument)} underlayColor='#00EE76'> </TouchableOpacity >
_ NeedHandlderArgument (argument) {// only process argument}