【问题标题】:How do I change the theme when I press the button? (React-native)按下按钮时如何更改主题? (反应原生)
【发布时间】:2020-09-18 10:29:02
【问题描述】:

当我按下按钮时,我希望它获得 button2 主题而不是 buttondefault。我该怎么做? (我在 react-native 文档中找不到,有链接吗?)

export default class deneme extends Component {
  constructor(props) {
    super(props);
    this.state = {
           

    
    };
  }


  render() {
    return (
      <View style={{flex:1, justifyContent:'center', alignItems:'center'}}>

        <TouchableOpacity 
        onPress={() => this.pressme()}
        style={styles.buttondefault}>
        <Text style={{fontSize:40}}> B </Text>
        </TouchableOpacity>

      </View>
    );
  }
}

const styles = StyleSheet.create({
    buttondefault:{
        width:100,
        height:50,
        backgroundColor:'green'
    },

    button2:{
        width:50,
        height:100,
        backgroundColor:'green'
    },


});

【问题讨论】:

    标签: android react-native android-button android-theme


    【解决方案1】:

    你可以为这个按钮设置一个状态,点击按钮会改变状态,然后会改变样式,如下所示:

    constructor(props) {
      super(props);
      this.state = {
        buttonStyle: 'default' 
      };
    }
    ...
    ...
    <TouchableOpacity 
      onPress={() => this.pressme()}
      style={(this.state.buttonStyle == 'default') ? styles.buttondefault : styles.button2}>
      <Text style={{fontSize:40}}> B </Text>
    </TouchableOpacity>
    ...
    ...
    pressme() {
      this.setState({buttonStyle: 'button2'});
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      相关资源
      最近更新 更多