将 onPress 函数绑定到 <Text>看法 [英] Bind onPress function to a <Text> view
本文介绍了将 onPress 函数绑定到 <Text>看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在不使用 TouchableOpacity 或 TouchableHighlight 的情况下将 onPress 事件绑定到 Text 视图的一部分,因为它们会用新行分隔文本.
I want to bind onPress event to a part of a Text view without using TouchableOpacity or TouchableHighlight because they break text with new lines.
有没有什么办法可以直接绑定到 React-native 中的 Text 元素?
is there any way to bind it directly to the Text element in React-native?
推荐答案
你可以这样使用:
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>
);
}
}
你可以试试这个小吃.
快乐编码:)
这篇关于将 onPress 函数绑定到 <Text>看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文