首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否禁用TouchableHighlight onPress?

是否禁用TouchableHighlight onPress?
EN

Stack Overflow用户
提问于 2015-10-22 22:34:27
回答 2查看 8.6K关注 0票数 5

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

EN

回答 2

Stack Overflow用户

发布于 2015-10-24 06:38:55

我解决了这个问题。下面是如何实现的:

首先,感谢James Ide提供了非常棒的带有disabled道具的button组件。

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

index.ios.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-10-23 20:30:16

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

代码语言:javascript
复制
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属性

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33283614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档