禁用TouchableHighlight onPress?

时间:2015-10-22 14:34:27

标签: reactjs react-native

如果其中一个被按下,如何禁用TouchableHighlight组?可能吗 ?例如,我有listview,每行有两个TouchableHighlights。如果其中一个被按下,我想禁用这两个可触摸的onPress功能。

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。方法如下:

首先,感谢James Idebutton道具的精美disabled组件。

我为按钮组编写了自定义组件 - main.js。并使用disabled道具禁用按钮组。

screenshot and logs

index.ios.js

class PNTest extends React.Component{
  constructor(props){
    super(props)

    this.state ={
      clicked: []
    }
  }

  render(){
    return(
      <View style={styles.container}>
        <Main handleClick={this._handleClick.bind(this)} left={1} right={2} name={'group1'}/>
        <Main handleClick={this._handleClick.bind(this)} left={3} right={4} name={'group2'}/>
        <Text style={styles.welcome}>
          {this.state.clicked}
        </Text>
      </View>
    );
  }

  _handleClick(id, group){
    this.state.clicked.push(id);
    console.log(group + ' now inactive and clicked button id: ' + id);
    console.log('clicked button ids: ' + this.state.clicked);
  }
}

main.js

class Main extends React.Component{
  constructor(props){
    super(props)

    this.state = {
      disabled: false,
      left: {},
      right: {}
    }
  }

  render(){
    return(
      <View style={styles.container}>
        <Button
          disabled={this.state.disabled}
          style={[styles.buttonContainer, this.state.left]}
          onPress={() => this._onPress(this.props.left)}>
          Left
        </Button>
        <Button
          disabled={this.state.disabled}
          style={[styles.buttonContainer,this.state.right]}
          onPress={() => this._onPress(this.props.right)}>
          Right
        </Button>
      </View>
    );
  }

  _onPress(id){
    var left = {};
    var right = {};

    if(id === this.props.left){
      left = styles.buttonSelected;
      right = styles.buttonNotSelected;
    }else if(id === this.props.right){
      right = styles.buttonSelected;
      left = styles.buttonNotSelected;
    }

    this.setState({
      disabled: true,
      left: left,
      right: right
    });

    this.props.handleClick(id, this.props.name);
  }
}

答案 1 :(得分:-1)

这里的一个选择是使用非常基本的信号量。单击其中一个按钮后,可以将全局变量x设置为true。在另一个onPress函数中,如果x为真,则可以返回

onPressOne(){
 if(this.semaphore)
   return
 this.semaphore = true
 // other code here
}
onPressTwo(){
 if(this.semaphore)
   return
 this.semaphore = true
 // other code here
}

在重置this.semaphore = false之前,按钮不会执行任何操作 如果要禁用TouchableHighlight中的视觉突出显示,可以在单击其中一个按钮时更改 activeOpacity 属性

相关问题