如何監(jiān)聽Text組件的點(diǎn)擊事件?
用TouchableOpacity包裝起來即可
export default class Test1 extends Component {
state={ //狀態(tài)機(jī),當(dāng)裏面的值改變的時(shí)候會(huì)重新運(yùn)行render()函數(shù),也就是相當(dāng)於ios開發(fā)中的刷新UI介面了
title:'默認(rèn)值'
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity activeOpacity={0.5}
onPress={()=>this.click('點(diǎn)擊')}//此處必須加()=>因?yàn)槭腔卣{(diào)
onPressIn={()=>this.click('按下')}
onPressOut={()=>this.click('抬起')}
onLongPress={()=>this.click('長按')}
>
<View>
<Text>{this.state.title}</Text>
</View>
</TouchableOpacity>
</View>
);
}
click(event){
this.setState({
title:event
})
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});