我正在使用react native制作一个android应用程序,我已经使用TouchableOpacity组件来创建按钮。 我使用一个文本输入组件来接受来自用户的文本,并且该按钮只应该在文本输入匹配某个字符串时启用。 我可以想到一种方法来做到这一点,即最初呈现按钮而不使用TouchableOpactiy包装器,并在输入字符串匹配后使用包装器重新呈现。 但我猜有更好的办法。有人能帮忙吗?


当前回答

TouchableOpacity接收activeOpacity。你可以这样做

<TouchableOpacity activeOpacity={enabled ? 0.5 : 1}>
</TouchableOpacity>

如果它被启用,它看起来会很正常,否则,它看起来就像touchable没有反馈。

其他回答

你可以启用和禁用按钮或使用条件或直接在默认情况下,它将是disable: true

 // in calling function of button 
    handledisableenable()
        {
         // set the state for disabling or enabling the button
           if(ifSomeConditionReturnsTrue)
            {
                this.setState({ Isbuttonenable : true })
            }
          else
          {
             this.setState({ Isbuttonenable : false})
          }
        }

<TouchableOpacity onPress ={this.handledisableenable} disabled= 
     {this.state.Isbuttonenable}>

    <Text> Button </Text>
</TouchableOpacity>

这个土生土长的基础有一个解决方案:

<Button
    block
    disabled={!learnedWordsByUser.length}
    style={{ marginTop: 10 }}
    onPress={learnedWordsByUser.length && () => {
      onFlipCardsGenerateNewWords(learnedWordsByUser)
      onFlipCardsBtnPress()
    }}
>
    <Text>Let's Review</Text>
</Button>

你可以用TouchableWithoutFeedback构建一个CustButton,并设置你想要的onPressIn, onpresout或其他道具的效果和逻辑。

因此,在react native中禁用任何按钮是非常容易的

<TouchableOpacity disabled={true}>
    <Text> 
          This is disabled button
   </Text>
</TouchableOpacity>

Disabled是react native中的一个道具,当你将它的值设置为“true”时,它将禁用你的按钮

快乐Cooding

使用禁用的true属性

<TouchableOpacity disabled={true}> </TouchableOpacity>