我花了大约2个小时,确实需要帮助。我想从我的数组中获取随机颜色,然后将backgroundColor设置为该一种颜色。我在那里复制了代码。https://jsfiddle.net/1x0k2ot4/
感谢您的每条建议。
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
inputValue: '',
backgroundColor: '',
dataSource: ds.cloneWithRows([]),
};
this._handleTextChange = this._handleTextChange.bind(this);
this._handleDeleteButtonPress = this._handleDeleteButtonPress.bind(this);
this._setColor = this._setColor.bind(this);
}
_setColor() {
const backgroundColor = backgroundcolors[Math.floor(Math.random()*backgroundcolors.length)];
this.setState({
backgroundColor: backgroundColor
})
}<View style={{ backgroundColor: this.state.backgroundColor, alignItems: 'center', height: 80, padding: 8, borderWidth: 1.5, borderColor: '#e0e0e0', flex: 1, flexDirection: 'row', marginBottom: 5,}}>
<Text style={styles.todoText}>{rowData}</Text>
<View>
<TouchableHighlight onPress={handleDelete} style={styles.crossCloseButton}>
<View>
<Image style={styles.imgClose} source={{uri: 'http://playground.davidfutera.cz/close.png'}}/>
</View>
</TouchableHighlight>
</View>
</View>
试试这个,您的帖子有点混乱,但是我认为这可以帮助您,如果出现错误告诉我,它应该可以工作
constructor(props) {
super(props);
this.state = {
bgColor: [
'red',
'blue',
'yellow',
],
selectedColor: '',
};
}
componentDidMount() {
this._getRandomColor()
}
_getRandomColor(){
var item = this.state.bgColor[Math.floor(Math.random()*this.state.bgColor.length)];
this.setState({
selectedColor: item,
})
}
<View style={{backgroundColor: this.state.selectedColor}}>
<Text>Test</Text>
</View>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句