【问题标题】:Adding checkbox in between text description in react native在反应原生的文本描述之间添加复选框
【发布时间】:2019-06-10 05:58:37
【问题描述】:

我有一个视图,其中显示了从服务器获取的标题和详细信息,如下所示。

现在,在问题描述中有一个标签__CB__,我需要将其替换为复选框,因此,将有一个复选框而不是该标签,其余文本将照常继续。我已根据标签分隔文本并在其间放置了一个复选框,但我无法对齐视图,要么它按列排列,要么如果我尝试按行排列,则文本不会继续。这是我尝试过的。

尝试1:

<ScrollView
            style={styles.scrollStyle}
            scrollEnabled={scrollEnabled}
            onContentSizeChange={this.onContentSizeChange}
          >
            <View style={styles.checkBoxTextContainer}>
              <Text style={styles.questionText}>{questionText1}</Text>
              <CheckBox
                style={styles.checkboxStyle}
                onClick={this.checkboxClick}
                isChecked={this.state.isChecked}
                checkedImage={<Image source={Images.checkBoxTick} />}
                unCheckedImage={<Image source={Images.checkBoxEmpty} />}
                // leftText={questionText1}
                // rightText={questionText2}
              />

               <Text style={styles.questionText}>{questionText2}</Text>
            </View>
          </ScrollView>
Styles
checkBoxTextContainer: {
    flex: 1,
    flexDirection: "row"
  },
  checkBoxStyle: {
    width: 20,
    height: 20
  }

结果:

尝试2:

<ScrollView
                style={styles.scrollStyle}
                scrollEnabled={scrollEnabled}
                onContentSizeChange={this.onContentSizeChange}
              >
                  <Text style={styles.questionText}>{questionText1}</Text>
                  <CheckBox
                    style={styles.checkboxStyle}
                    onClick={this.checkboxClick}
                    isChecked={this.state.isChecked}
                    checkedImage={<Image source={Images.checkBoxTick} />}
                    unCheckedImage={<Image source={Images.checkBoxEmpty} />}
                    // leftText={questionText1}
                    // rightText={questionText2}
                  />

                   <Text style={styles.questionText}>{questionText2}</Text>
              </ScrollView>

结果:

我需要复选框在文本之间继续,如原始图像中的标签。任何帮助表示赞赏。

【问题讨论】:

    标签: react-native flexbox styles react-native-android react-native-ios


    【解决方案1】:

    以下是您的场景的示例工作代码。

    import React, { Component } from 'react'
    import { View, Text, StyleSheet } from 'react-native'
    
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            flexDirection: 'row',
            flexWrap: 'wrap'
        },
        cb: {
            width: 10,
            height: 10,
            borderWidth: 2,
            borderRadius: 2,
            borderColor: 'red',
            marginTop: 4,
            marginHorizontal: 3,
        }
    })
    
    const sampleData = [ 'Lorem', 'ipsum', 'dolor', 'sit', 'amet',
    'CB', 
    'consectetur', 'adipiscing', 'elit.',  'Curabitur',
    'CB',
    ' nunc', 'vel', 'scelerisque', 'tempus.', 'CB', 'Morbi', 'luc', 'abcd', 'xyzw'
    ]
    
    const CheckBox = () => (
        <View style={styles.cb} />
    )
    
    export default class CheckText extends Component {
        renderData = (data) => {
            const views = data.map((item) => {
                if (item === 'CB') { return <CheckBox />}
                return (<Text>{item}</Text>)
            })
            return views
        }
    
        render() {
            return (
                <View style={styles.container}>
                    {this.renderData(sampleData)}
                </View>
            )
        }
    }
    

    这是输出的快照

    注意

    我已将文本解析为字符串数组。这是这里的关键逻辑。您决定对输入文本进行标记的方式将影响组件的呈现。当我尝试将长字符串作为令牌时,flexWrap: 'wrap' 无法正常工作。有关更多详细信息,请参阅此issue。标记为单个词可能会完成这项工作。

    【讨论】:

      【解决方案2】:

      尝试将文本和复选框包装到&lt;Text&gt; 标记中。 请尝试以下解决方案。它可能会帮助你。

              <ScrollView
                  style={styles.scrollStyle}
                  scrollEnabled={scrollEnabled}
                  onContentSizeChange={this.onContentSizeChange}
                >
                  <View style={{
                       width:'100%',
                       flexDirection: "row"
                  }}>
                    // wrap into text tag
                    <Text>
                    // add space after first text
                    <Text style={styles.questionText}>{questionText1+" "}</Text>
                    <CheckBox
                      style={styles.checkboxStyle}
                      onClick={this.checkboxClick}
                      isChecked={this.state.isChecked}
                      checkedImage={<Image source={Images.checkBoxTick} />}
                      unCheckedImage={<Image source={Images.checkBoxEmpty} />}
                    />
                     // add space before second text
                     <Text style={styles.questionText}>{" "+questionText2}</Text>
                    </Text>
                  </View>
                </ScrollView>
      

      【讨论】:

      • 这给出了一个错误,指出当前不支持在 中嵌套 。我正在使用反应原生版本 0.57.8
      • 我已经在 iOS 和 react-native 版本 0.55.3 中测试过了。我认为嵌套视图仅在 iOS 中支持。 facebook.github.io/react-native/docs/text#nested-views-ios-only让我检查另一个解决方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      相关资源
      最近更新 更多