切换按钮以控制按钮

时间:2019-10-08 13:47:23

标签: javascript react-native

我将实现一个信息隐私页面。

我已经做到了:

class Informativa extends Component {
    constructor(props) {
        super(props);
        this.state = {
            switchValue: false,
            permission: ""
        }
    }

    toggleSwitch = value => {
        this.setState({ switchValue: value });
      };

  render() {
    return(
// text about privacy

<Switch
                style={{ marginTop: 30, marginRight: 20}}
                onValueChange={this.toggleSwitch}
                value={this.state.switchValue}
            />
            {(!this.state.switchValue)
            ?
            <TouchableOpacity  
                style={ [style.button, style.buttonOK]}>
                <Text style={[style.buttonTesto]}>Accept</Text>
            </TouchableOpacity>
            :
            <TouchableOpacity  disabled={true}
                style={ [style.button, style.buttonOK]}>
                <Text style={[style.buttonTesto]}>Accept</Text>
            </TouchableOpacity>}
        </View>

我会意识到就像是一个开关,当它在“开”启用按钮中时,当它在“关”禁用按钮时。

此刻什么都不要改变。

我该怎么办?

2 个答案:

答案 0 :(得分:1)

自从我写了ReactNative以来已经有一段时间了。如果Switch像许多html输入一样接受禁用的属性,则只要switchValue为布尔值,就可以解决问题。

// Switch disabled when switchValue == false (off)

<Switch style={{ marginTop: 30, marginRight: 20}}
        onValueChange={this.toggleSwitch}
        value={this.state.switchValue}
        disabled={this.state.switchValue}/>

答案 1 :(得分:0)

<Switch
       style={{ marginTop: 30, marginRight: 20}}
       onValueChange={this.toggleSwitch}
       value={this.state.switchValue}
       {this.state.switchValue ? disabled :  ""}
        />

类似的东西有用吗?