reactjs - 如何在 Text ReactNative 中包含 TouchableOpacity

标签 reactjs react-native text view touchableopacity

您好,我想将 TouchableOpacity 包装在文本中,因为我想让某些文本可单击。当我将所有内容都包含在文本中时,它看起来很完美,这就是我想要的样子。

 <Text 
   style={{color: colors.black,
           fontSize: 12,
           fontWeight: 'normal',
           marginTop: 10,
           lineHeight: 18}}>
      {strings.loginPrivacyTermsCondOne} 
        <Text style={{color: colors.primaryColor,
                      fontSize: 12,
                      fontWeight: 'normal',}}>
          {strings.loginPrivacyTermsCondTwo}
        </Text>
          {strings.loginPrivacyTermsCondThree} 
        <Text style={{color: colors.primaryColor,
                      fontSize: 12,
                      fontWeight: 'normal'}}>
          {strings.loginPrivacyTermsCondFour}
        </Text>

          {/* <TouchableOpacity onPress={ this.termsOfService }>
            <Text style={{color: colors.primaryColor,
                          fontSize: 12,
                          fontWeight: 'normal',}}>
              {strings.loginPrivacyTermsCondFour}
            </Text>
          </TouchableOpacity> */}
      </Text>

当我添加 TouchableOpacity 时,它不起作用。

我尝试将其添加到 View 中,然后它工作正常,并且我能够添加 TouchableOpacity,但从 UI 角度来看,它们没有正确对齐。

这是仅显示文本的屏幕截图,其中 TouchableOpacity 不起作用,第二位位于 View 中,其中 TouchableOpacity 起作用,但看起来不正确。

enter image description here

怎样才能让它看起来像第一位一样。非常感谢任何建议。

谢谢

最佳答案

您可以嵌套 Text 元素,并在要使其可按下的每个嵌套 Text 元素(链接)上分配 onPress 处理程序。

见下文。有一个外部文本元素,内部是另一个文本元素,子文本元素有一个 onPress 处理程序,如果运行它,你会看到“但是这是!”当您按下时执行 onPress 处理程序,无需 Touchable* 元素。

<Text style={{color: '#000'}}> 
     This part is not clickable 
     <Text onPress={() =>
           {alert('but this is');}}
           style={{color: '#00F'}}>
     But this is!
     </Text> 
     but this isn't
</Text>

您可以创建一个样式来放置在任何具有 onPress 处理程序的 Text 元素上,以使它们具有不同的颜色,如示例图像中所示。

请注意,这很像 HTML,例如,您可以将 anchor 标记嵌套在 p 元素内;

<p>
    This part is not clickable 
    <a href="https://google.com"> but this is</a>
    but this isn't
</p>

在您的示例中,它会是这样的(未经测试):

<Text style={{color: colors.black,
              fontSize: 12,
              fontWeight: 'normal', 
              marginTop: 10,
              lineHeight: 18}}>
        {strings.loginPrivacyTermsCondOne} 
        <Text style={{color: colors.primaryColor,
                      fontSize: 12,
                      fontWeight: 'normal',}}>
          {strings.loginPrivacyTermsCondTwo}
        </Text>
          {strings.loginPrivacyTermsCondThree} 
        <Text style={{color: colors.primaryColor, 
                      fontSize: 12,
                      fontWeight: 'normal'}}>
          {strings.loginPrivacyTermsCondFour}
        </Text>

          <Text onPress={ this.termsOfService } 
                style={{color: colors.primaryColor,
                        fontSize: 12, 
                        fontWeight: 'normal'}}>
              {strings.loginPrivacyTermsCondFour}
          </Text> 
      </Text>

为了回应您的评论,这里有一个在单击链接后更改颜色的简化示例。

简而言之,我在状态上添加了一个 bool 字段,一旦按下文本,我就会将该状态变量更新为 true,然后文本元素的样式值有一个三元运算符,它决定渲染文本的颜色在我的示例中,如果尚未按下,它将显示为“colors.primaryColor”,单击后将显示为“red”。

class Foo extends Component {

    constructor (props) {
        super(props);

        this.state = {
            privacyClicked: false //Track if they have clicked privacy
        };
    }

    render () {

        return (
             <Text onPress={ () =>{
    this.setState({
        privacyClicked: true
    });
}} style={{color: (this.state.privacyClicked ? colors.primaryColor : 'red'), 
           fontSize: 12,
           fontWeight: 'normal'}}>
    {strings.loginPrivacyTermsCondFour}
</Text> 
        );
    }
}

PS,示例文本的格式不太好。

关于reactjs - 如何在 Text ReactNative 中包含 TouchableOpacity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52168451/

相关文章:

react-native - 切换 React Native 元素的可见性?

java - 尝试将数组的克隆保存在 txt 文件中但返回 NULL

reactjs - 有什么方法可以将 Saga 与新的 React 钩子(Hook)一起使用?

reactjs - React router 链接到条件渲染按钮

javascript - 这是什么奇怪的: JSON Parse error: Unexpected identifier "Tunnel"

c - 尝试读取带有表情符号的文本文件并打印出来

text - LaTeX 中的缩进自动换行

javascript - 将组件实例直接传递给 prop 还是使用渲染回调?

reactjs - 需要使用react-redux进行身份验证的延迟加载组件

react-native - React-Query:单击按钮时如何使用Query