从数组设置随机backgroundColor

大卫·富特拉

我花了大约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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

随机设置 <Avatar> backgroundColor

如何为遍历数组的每组元素设置随机颜色?

将变量设置为数组中的随机项

从数组中选择随机值并使用 Javascript 设置 RGB 颜色

尝试将按钮背景设置为数组中的随机颜色

选择数组的随机元素以设置Youtube videoId API

如何以编程方式设置WKInterfaceController的backgroundColor?

设置自定义NSView的backgroundColor

如何设置UITableView空白空间的backgroundColor不同?

设置视图的backgroundColor时如何避免平铺?

如何在Android的ImageView上设置BackgroundColor?

为数组中的按钮随机设置图像,并从数组中获取按钮的cgrect

如何更改从数组动态创建的 TouchableOpacity 的 backgroundColor?

如何从指令中设置输入类型文本的值,该指令从数组中获取随机值

将变量设置为数组中的随机项目时,如何指定机会?

如何将numpy数组中的元素随机设置为0

如果键值存在,则在多维数组中随机取消设置元素

如何在2d numpy数组中设置随机元素而不循环?

斯威夫特:如何设置不带参数的函数以返回数字的随机数组?

使用指向结构的指针数组将结构的元素设置为随机双精度值

将变量设置为来自数组的随机元素,而不是元素值。 - -Python - -

如何在 discord.js (javascript) 中使用数组设置随机播放状态

如何设置随机颜色

设置随机图像

模拟数组的随机迭代

数组的随机改组

从数组获取随机ID

随机对象数组

从数组中随机选择