如果按下其中之一,如何禁用 TouchableHighlight 组?是否可以 ?例如我有 ListView ,每一行都有两个 TouchableHighlights。如果按下其中之一,我想禁用这两个可触摸的 onPress 功能。

最佳答案

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

首先,感谢 James Ide 使用 button Prop 提供了很棒的 disabled 组件。

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

reactjs - 禁用 TouchableHighlight onPress?-LMLPHP
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);
  }
}

关于reactjs - 禁用 TouchableHighlight onPress?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33283614/

10-12 00:06
查看更多