簡體   English   中英

React-Native:如何更改后台第二個動作

[英]React-Native: How to change background second action

我認為問題很簡單,但我似乎無法弄明白。 我使用react-native-router-flux庫和NativeBase庫作為按鈕。

這是我的代碼:

<Button transparent onPress={Actions.MainOne } style={{ width: 50, height: 50 }} >
       <Text>Option1</Text>
   </Button>

  <Button transparent onPress={Actions.MainTwo} style={{ width: 50, height: 50 }} >
           <Text>Option2</Text>
    </Button>

每當我按下它並且它處於活動狀態時我想改變按鈕的背景顏色。 如果我點擊另一個按鈕,那么我剛按下的按鈕會獲得背景,第一個按鈕會回到正常的透明背景。 我不確定如何向按鈕添加兩個動作。 如果有人可以提供幫助我會很感激。 我不需要使用按鈕庫,所以歡迎任何關於此的想法!

謝謝 !

我使用flux-router來瀏覽場景。 這是按下按鈕時更改按鈕背景顏色的方法:

    constructor() {
      super();
      state = {
        color1: 'transparent',
        color2: 'transparent',
      }
    }

    setActive(button) {
      if (button === 1) {
        if (this.state.color1 === 'transparent') {
          this.setState({ 
            color1: 'red',
            color2: 'transparent' 
          })
        }
      } else {
        if (this.state.color2 === 'transparent') {
          this.setState({ 
            color2: 'red', 
            color1: 'transparent' 
          })
        }
      }
    }

    { . . . }

    <Button
        onPress={this.setActive.bind(this, 1)}
        style={{ width: 50, height: 50, backgroundColor: this.state.color1 }} 
    >
        <Text>Option1</Text>
    </Button>

    <Button 
        this.setActive.bind(this, 2)
        style={{ width: 50, height: 50, backgroundColor: this.state.color2 }} 
    >
       <Text>Option2</Text>
    </Button>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM