使用TouchableOpacity組件
實現單擊事件只需要聲明onPress屬性即可,其他同理,實現onPressIn,onPressOut,onLongPress
constructor(props){ super(props); this.state = { title:'不透明觸摸' } this.activiEvent = this.activiEvent.bind(this); } render() { return ( <View> {/* onPress={this.renderPress()} onPress={()=>this.activiEvent('點擊')} onPressIn={()=>this.activiEvent('按下')} onPressOut={()=>this.activiEvent('抬起')} onLongPress={()=>this.activiEvent('長按')} */} <View> <TouchableOpacity activeOpacity={0.5} onPress={()=>this.activiEvent('點擊')} onPressIn={()=>this.activiEvent('按下')} onPressOut={()=>this.activiEvent('抬起')} onLongPress={()=>this.activiEvent('長按')} > <View> <Text>我是文本但可以點擊常用事件</Text> </View> </TouchableOpacity> </View> <View> <Text>{this.state.title}</Text>{/*標簽內取值要用花括號*/} </View> </View> ); } renderPress(){ {/*還沒點擊就彈窗了 花括號注釋不能寫到函數外 標簽內注釋用花括號 標簽外可以用// */} Alert.alert('iOS') } activiEvent(event){ this.setState({ title:event }) }