如何在多个按钮React Native中更改按下的颜色按钮

时间:2018-01-25 12:57:56

标签: javascript react-native

我的本​​机应用程序中有3个按钮。当用户单击按钮1时,我需要将其颜色更改为橙​​色。但其他按钮应具有默认颜色(灰色)。如果用户下次单击按钮3,颜色应更改为橙色,但第一个按钮颜色应重置为默认值。我对原生的反应是全新的,这就是我的尝试。但它适用于所有按钮。我知道如果我可以拥有具有唯一ID的多个状态,则可以完成。但我不知道这种方法。

<Text style={ styles.switchButtonsTitle }>Choose Type of User</Text>
<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("BASIC")} >
    <Text style={_style}>
        <Text style={styles.switchButtonsText}>BASIC</Text>
    </Text>
</TouchableOpacity>


<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("INTERMEDIATE")}>
    <Text style={_style}>
        <Text style={styles.switchButtonsText}>INTERMEDIATE</Text>
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("ADVANCED")}>
    <Text style={{backgroundColor: this.state.backgroundColor}}>
        <Text style={styles.switchButtonsText}>ADVANCED</Text>
    </Text>
</TouchableOpacity>

selectionOnPress

selectionOnPress(userType) {
    this.setState({
        onClicked: true
    });
} 

道具

constructor(props) {
    super(props);
    this.state = {
        onClicked: false
    }
    this.selectionOnPress = this.selectionOnPress.bind(this)
}

渲染(不添加所有代码,只添加了此帖子的有用代码)

render() {
    var _style;
    if (this.state.onClicked) { // clicked button style
        _style = {
            backgroundColor: "red"
        }
    }
    else { // default button style
        _style = {
            backgroundColor: "blue"
        }
    }

3 个答案:

答案 0 :(得分:6)

我对你的代码做了一些修改

 export default class App extends Component {
constructor(props) {
    super(props);
    this.state = { selectedButton: null };
    this.selectionOnPress = this.selectionOnPress.bind(this);
}

selectionOnPress(userType) {
    this.setState({ selectedButton: userType });
}

render() {
    return (
        <View>
            <Text style={styles.switchButtonsTitle}>
                Choose Type of User
            </Text>
            <TouchableOpacity
                onPress={() => this.selectionOnPress("BASIC")}
            >
                <Text
                    style={{
                        backgroundColor:
                            this.state.selectedButton === "BASIC"
                                ? "red"
                                : "grey"
                    }}
                >
                    <Text style={styles.switchButtonsText}>BASIC</Text>
                </Text>
            </TouchableOpacity>

            <TouchableOpacity
                onPress={() => this.selectionOnPress("INTERMEDIATE")}
            >
                <Text
                    style={{
                        backgroundColor:
                            this.state.selectedButton === "INTERMEDIATE"
                                ? "red"
                                : "grey"
                    }}
                >
                    <Text style={styles.switchButtonsText}>
                        INTERMEDIATE
                    </Text>
                </Text>
            </TouchableOpacity>

            <TouchableOpacity
                onPress={() => this.selectionOnPress("ADVANCED")}
            >
                <Text
                    style={{
                        backgroundColor:
                            this.state.selectedButton === "ADVANCED"
                                ? "red"
                                : "grey"
                    }}
                >
                    <Text style={styles.switchButtonsText}>
                        INTERMEDIATE
                    </Text>
                </Text>
            </TouchableOpacity>
        </View>
    );
}
}


... don't forget to define your styles

答案 1 :(得分:0)

首先,我建议您创建一个按钮组件。 https://medium.com/the-react-native-log/organizing-a-react-native-project-9514dfadaa0

一种简单的方法是为每个按钮创建一个状态,如

<TouchableOpacity onPress={
        (userType) => this.selectionOnPress("BASIC");
        this.setState({backgroundColor1: this.state.backgroundColor1 == 'grey'? 'orange' : 'grey'});
}>
    <Text style={{backgroundColor: this.state.backgroundColor1}}>
        <Text style={styles.switchButtonsText}>BASIC</Text> 
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={
    (userType) => this.selectionOnPress("INTERMEDIATE");
    this.setState({backgroundColor2: this.state.backgroundColor2 == 'grey'? 'orange' : 'grey'});
}>
    <Text style={{backgroundColor: this.state.backgroundColor2}>
        <Text style={styles.switchButtonsText}>INTERMEDIATE</Text> 
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={
    (userType) => this.selectionOnPress("ADVANCED");
    this.setState({backgroundColor3: this.state.backgroundColor3 == 'grey'? 'orange' : 'grey', backgroundColor1: 'grey'});
}>
    <Text style={{backgroundColor: this.state.backgroundColor}}>
        <Text style={styles.switchButtonsText}>ADVANCED</Text> 
    </Text>
</TouchableOpacity>

答案 2 :(得分:-1)

简化示例:

//jsx
// asign default class name to your buttons with desired default color,
// onclick change said class to active css class which will have different background color
   <div>
    <button onClick={this.handleClick} className='btn-default'>basic</button>
    <button onClick={this.handleClick} className='btn-default'>intermediate</button>
    <button onClick={this.handleClick} className='btn-default'>advance</button>
  </div>

//js
// this method select parent element of your button which is also parent
// element for other buttons and then gets all other buttons in button 
// variable via children property
  handleClick(event) {
    const buttons = event.target.parentElement.children;
    for(let i = 0; i < buttons.length; i++) {
        //here you set all buttons to default color
        buttons[i].classList.add('btn-default');
        buttons[i].classList.remove('btn-active');
    }
    //here you add active class(color) to button you originally clicked
    event.target.classList.add('btn-active');
  }

  //css

.btn-default {
   background-color: grey;
 }

.btn-active {
   background-color: orange;
 }