im试图列出反应本机可触摸高亮,当按下时,颜色会改变,但不会触发onpress功能。 这是可触摸的亮点
const DeviceList = ({ devices, connectedId, showConnectedIcon, onDevicePress }) =>
<ScrollView style={styles.containerscroll}>
<View style={styles.listContainer}>
{devices.map((device,i) => {
return (
<TouchableHighlight
underlayColor='#DDDDDD'
key={`${device.id}_${i}`}
style={styles.listItem} onPress={() => onDevicePress(device)}>
<View style={{ justifyContent: 'space-between', flexDirection: 'row', alignItems: 'center' }}>
<Text style={{ fontWeight: 'bold' }}>{device.name}</Text>
<Text>{`<${device.id}>`}</Text>
</View>
</TouchableHighlight>
)
})}
</View>
</ScrollView>
然后我在渲染器中调用
<View style = {styles.container1}>
<DeviceList
showConnectedIcon={this.state.section === 1}
connectedId={this.state.device && this.state.device.id}
devices={this.state.devices}
onDevicePress={(device) => this.onDevicePress1(device)} />
</View>
这就是我称为onDevicePress1(device)
onDevicePress1 =(device) =>{
if (this.state.section === 0) {
this.setState({ connecting: true })
console.log(device)
console.log("device click") //this is not showing on the console
BluetoothSerial.connect(device.id)
.then((res) => {
console.log(`Connected to device ${device.name}`)
this.setState({ device, connected: true, connecting: false })
// this.listener = EventRegister.addEventListener('myCustomEvent',
// BluetoothSerial.readFromDevice()
// .then((data) => {console.log(data)})
// )
setInterval(function a(){
BluetoothSerial.readFromDevice()
.then((data) => {console.log(data)})
}, 1000)
})
.catch((err) => console.log(err.message))
console.log('connecting to ',device.id)
//console.log(this.state.devices)
} else {
this.pairDevice(device)
}
}