【发布时间】:2017-02-01 19:42:19
【问题描述】:
我正在尝试制作一个每次点击屏幕时背景颜色都会发生变化的应用。我有点击事件工作,但我不知道如何更改背景颜色。
这是我现在的代码:
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
} from 'react-native';
let randomHex = () => {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
export default class randomBackground extends Component {
constructor(props) {
super(props)
this.onClick = this.onClick.bind(this)
}
onClick() {
console.log('clicked ')
}
render() {
return (
<TouchableHighlight onPress={ this.onClick } style={styles.container}>
<View>
<Text style={styles.instructions}>
Tap to change the background
</Text>
</View>
</TouchableHighlight>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: randomHex()
},
instructions: {
color: "white"
}
});
AppRegistry.registerComponent('randomBackground', () => randomBackground);
我希望每次点击屏幕时都重新生成背景。
【问题讨论】: