【问题标题】:React Native Dynamically Change View’s Background ColorReact Native 动态改变 View 的背景颜色
【发布时间】: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);

我希望每次点击屏幕时都重新生成背景。

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    您可以使用this.setState更改背景颜色

    constructor 中设置初始背景颜色

    this.state = {
        backgroundColor: randomHex()
    }
    

    在您的 render 函数中将您的样式属性更改为:

    [styles.container, {backgroundColor: this.state.backgroundColor}] 
    

    onClick添加

    this.setState({backgroundColor: randomHex()});
    

    完整代码

    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);
    
            this.state = {
                backgroundColor: randomHex(),
            };
        }
    
        onClick() {
            console.log("clicked ");
            this.setState({ backgroundColor: randomHex() });
        }
    
        render() {
            return (
                <TouchableHighlight
                    onPress={this.onClick}
                    style={[
                        styles.container,
                        { backgroundColor: this.state.backgroundColor },
                    ]}
                >
                    <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);
    

    【讨论】:

    • 可能想尝试 TouchableWithoutFeedback 或 TouchableOpacity 而不是 TouchableHighlight。首先,不显示任何不透明度变化。其次,显示反馈但表现更好。
    • 由于某种原因,在我的情况下这会导致无限循环。唯一的区别是我在 Native Base ListItem 中使用 TouchableHighlight
    【解决方案2】:
    import {
        AppRegistry,
        StyleSheet,
        Text,
        View,
        TouchableHighlight
    } from 'react-native';
    
    
    
    export default class randomBackground extends Component {
    
        state={
          currentColor:"#FFFFF"
        }
    
        onClick() {
          let letters = '0123456789ABCDEF';
          let color = '#';
          for (let i = 0; i < 6; i++ ) {
             color += letters[Math.floor(Math.random() * 16)];
          }
          this.setState({currentColor:color})
        }
        render() {
            return (
                <TouchableHighlight onPress={ this.onClick.bind(this) } {[styles.container, {backgroundColor: this.state.currentColor}]}>
                    <View>
                        <Text style={styles.instructions}>
                            Tap to change the background
                        </Text>
                    </View>
                </TouchableHighlight>
            );
        }
    }
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center'
        },
        instructions: {
            color: "white"
        }
    });
    AppRegistry.registerComponent('randomBackground', () => randomBackground);
    

    【讨论】:

    【解决方案3】:

    当你只是想改变系列按钮样式的改变。 (例如标签栏按钮,一个按钮选择其他不选择) 只需使用条件样式

    style={[this.state.yourstate ? styles.selectedButtonStyle : styles.normalButtonStyle]}
    

    【讨论】:

    • 我需要检查 5 个条件,那么我该如何使用这个解决方案?请解释一下。
    • 你可以使用像 style={[this.state.firstCondition ? styles.selectedButtonStyle : this.state.secondCondition? : styles.anotherStye :styles.normalButtonStyle]} 这样的嵌套条件,但不确定它如何影响渲染性能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2021-11-23
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多