简体   繁体   English

在 React Native 中更改 TouchableOpacity 的颜色

[英]Change color of TouchableOpacity in React Native

Can anyone help me.谁能帮我。 This is my source code: https://snack.expo.io/rJFgyPDpH这是我的源代码: https : //snack.expo.io/rJFgyPDpH

Idea is that, if I click to " 1 Button " it should be 'red' and if I click to "2 Button" is also should change its color to 'red' but the "1 Button" should be changed to its default colour which is black.想法是,如果我点击“ 1按钮”它应该是“红色”,如果我点击“2按钮”也应该将其颜色更改为“红色”但“1按钮”应该更改为其默认颜色这是黑色的。 However, "2 Button" .但是, “2 按钮”

If my approach is too simple, other methods (such as TouchableHighlight , ES6 and etc) are also welcomed.如果我的方法太简单,也欢迎其他方法(例如TouchableHighlight 、 ES6 等)。 I appreciate if you show me mistakes so that I learn from that.我很感激你告诉我错误,以便我从中吸取教训。

Try below试试下面


state={
    selectedButton: '',
};

      <View style={styles.container}>
          <TouchableOpacity
              style={{ backgroundColor: this.state.selectedButton === 'button1' ? 'red' : 'black', padding: 15}}
              onPress={() => this.setState({ selectedButton: 'button1' })}
          >
            <Text style={styles.text}>1 Button</Text>
          </TouchableOpacity>

          <TouchableOpacity
              style={{ backgroundColor: this.state.selectedButton === 'button2' ? 'red' : 'black', padding: 15}}
              onPress={() => this.setState({ selectedButton: 'button2' })}
          >
            <Text style={styles.text}>2 button!</Text>
          </TouchableOpacity>

      </View>

You can write your code like:您可以编写如下代码:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button,TouchableOpacity} from 'react-native';

export default class App extends Component {
  state={
    backgroundColor: 'black',
    backgroundColor2: 'black',
    pressed: false,
  };

  changeColor(){
    if(!this.state.pressed){
       this.setState({ pressed: true,backgroundColor: 'red', backgroundColor2: 'black'});
    } else {
      this.setState({ pressed: false, backgroundColor: 'black' ,backgroundColor2: 'red'});
    }
  }
  render() {
    return (
      <View style={styles.container}>
          <TouchableOpacity
              style={{backgroundColor:this.state.backgroundColor, padding: 15}}
              onPress={()=>this.changeColor()}
                >
            <Text style={styles.text}>1 Button</Text>
          </TouchableOpacity>

          <TouchableOpacity
              style={{backgroundColor:this.state.backgroundColor2, padding: 15}}
              onPress={()=>this.changeColor()}
                >
            <Text style={styles.text}>2 button!</Text>
          </TouchableOpacity>

      </View>
    );
  }
}

const styles = StyleSheet.create({
  text:{
    color:'white'
    },
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff',
  },
});

Now If you click to the first button it should be 'red', but second, remains as 'black' background.现在,如果您单击第一个按钮,它应该是“红色”,但第二个按钮仍然是“黑色”背景。 Consequently, if you click to second button it should be 'red', whereas the first button should be 'black'.因此,如果您单击第二个按钮,它应该是“红色”,而第一个按钮应该是“黑色”。

changeColor=()=>{
   this.setState({
      backgroundColor:'red',
      backgroundColor2:'black'
   })
 }

  changeColor2=()=>{
    this.setState({
       backgroundColor:'black',
       backgroundColor2:'red'
   })
 }

As per your requirements, onPress on first button, it will invoke changeColor.根据您的要求,按第一个按钮,它将调用 changeColor。 And onPress of second button, it'll invoke changeColor2.并且在按下第二个按钮时,它会调用 changeColor2。

In the code, onPress of second button, it can be changed to changeColor2 instead of changeColor function.在代码中,第二个按钮的onPress,可以改为changeColor2而不是changeColor函数。

This这个

 onPress={()=>this.changeColor2()}

instead of代替

  onPress={()=>this.changeColor()}

By passing id you can change color alternatively通过传递 id,您可以交替更改颜色

import React, { Component } from 'react';
import { StyleSheet, TouchableOpacity, Text, View, Button } from 'react-native';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = { colorId:0 };
  }
  onPress = (id) => {
    this.setState({colorId: id});
  };

  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity
          style={this.state.colorId === 1? styles.red : styles.button}
          onPress={()=>this.onPress(1)}>
          <Text>Button1</Text>
        </TouchableOpacity>
        <TouchableOpacity
          style={this.state.colorId === 2? styles.red : styles.button}
          onPress={()=>this.onPress(2)}>
          <Text>Button2</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingHorizontal: 10,
  },
  red: {
    backgroundColor: 'red',
    alignItems: 'center',
    padding: 10,
  },
  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10,
  },
});

Live demo现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM