• <small id='D4F31'></small><noframes id='D4F31'>

    <i id='D4F31'><tr id='D4F31'><dt id='D4F31'><q id='D4F31'><span id='D4F31'><b id='D4F31'><form id='D4F31'><ins id='D4F31'></ins><ul id='D4F31'></ul><sub id='D4F31'></sub></form><legend id='D4F31'></legend><bdo id='D4F31'><pre id='D4F31'><center id='D4F31'></center></pre></bdo></b><th id='D4F31'></th></span></q></dt></tr></i><div id='D4F31'><tfoot id='D4F31'></tfoot><dl id='D4F31'><fieldset id='D4F31'></fieldset></dl></div>

        <legend id='D4F31'><style id='D4F31'><dir id='D4F31'><q id='D4F31'></q></dir></style></legend>
        <tfoot id='D4F31'></tfoot>

        • <bdo id='D4F31'></bdo><ul id='D4F31'></ul>
      1. 在 React Native 中更改 TouchableOpacity 的颜色

        时间:2023-11-29

            • <bdo id='cFLgl'></bdo><ul id='cFLgl'></ul>
                  <tbody id='cFLgl'></tbody>

                <small id='cFLgl'></small><noframes id='cFLgl'>

                • <i id='cFLgl'><tr id='cFLgl'><dt id='cFLgl'><q id='cFLgl'><span id='cFLgl'><b id='cFLgl'><form id='cFLgl'><ins id='cFLgl'></ins><ul id='cFLgl'></ul><sub id='cFLgl'></sub></form><legend id='cFLgl'></legend><bdo id='cFLgl'><pre id='cFLgl'><center id='cFLgl'></center></pre></bdo></b><th id='cFLgl'></th></span></q></dt></tr></i><div id='cFLgl'><tfoot id='cFLgl'></tfoot><dl id='cFLgl'><fieldset id='cFLgl'></fieldset></dl></div>
                  <legend id='cFLgl'><style id='cFLgl'><dir id='cFLgl'><q id='cFLgl'></q></dir></style></legend>

                • <tfoot id='cFLgl'></tfoot>
                  本文介绍了在 React Native 中更改 TouchableOpacity 的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能帮帮我.这是我的源代码:https://snack.expo.io/rJFgyPDpH

                  Can anyone help me. This is my source code: https://snack.expo.io/rJFgyPDpH

                  想法是,如果我点击1 Button"它应该是红色",如果我点击2 Button"也应该改变它的颜色为红色",但 "1 Button" 应更改为其默认颜色,即黑色.但是,2 按钮".

                  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. However, "2 Button".

                  如果我的方法过于简单,也欢迎使用其他方法(如TouchableHighlight、ES6 等).如果您向我展示错误以便我从中吸取教训,我将不胜感激.

                  If my approach is too simple, other methods (such as TouchableHighlight, ES6 and etc) are also welcomed. I appreciate if you show me mistakes so that I learn from that.

                  推荐答案

                  你可以这样写代码:

                  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'.

                  这篇关于在 React Native 中更改 TouchableOpacity 的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:React Native 上传图片失败 下一篇:Expo + React Native:在两种视图的坐标之间画线

                  相关文章

                • <legend id='6VEpE'><style id='6VEpE'><dir id='6VEpE'><q id='6VEpE'></q></dir></style></legend>

                    <tfoot id='6VEpE'></tfoot>
                  1. <i id='6VEpE'><tr id='6VEpE'><dt id='6VEpE'><q id='6VEpE'><span id='6VEpE'><b id='6VEpE'><form id='6VEpE'><ins id='6VEpE'></ins><ul id='6VEpE'></ul><sub id='6VEpE'></sub></form><legend id='6VEpE'></legend><bdo id='6VEpE'><pre id='6VEpE'><center id='6VEpE'></center></pre></bdo></b><th id='6VEpE'></th></span></q></dt></tr></i><div id='6VEpE'><tfoot id='6VEpE'></tfoot><dl id='6VEpE'><fieldset id='6VEpE'></fieldset></dl></div>
                      <bdo id='6VEpE'></bdo><ul id='6VEpE'></ul>

                  2. <small id='6VEpE'></small><noframes id='6VEpE'>