将onPress函数绑定到<text>视图

时间:2018-03-18 05:15:18

标签: react-native

我想将onPress事件绑定到Text视图的一部分,而不使用TouchableOpacity或TouchableHighlight,因为它们会用新行中断文本。

有没有办法将它直接绑定到React-native中的Text元素?

1 个答案:

答案 0 :(得分:3)

您可以使用以下内容:

import React,{Component} from 'react';
import { View, Text, Alert} from 'react-native';

export default class SliderContainer extends Component {

  render() {
    return (
      <View style={{marginTop: 50}}>
       <Text>
        {`TextPartOne
        `}
        <Text onPress={() => Alert.alert(
  'Alert Title',
  'My Alert Msg',
  [
    {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
    {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
    {text: 'OK', onPress: () => console.log('OK Pressed')},
  ],
  { cancelable: false }
)}>
            {`Nested text to press`}
        </Text>
        {`
        TextPartTwo`}
    </Text>
      </View>
    );
  }
}

您可以尝试this snack

快乐编码:)