【问题标题】:How to set Two inputs on same row in react native ?如何在本机反应中设置同一行上的两个输入?
【发布时间】:2017-08-22 14:03:48
【问题描述】:

嘿,我想在同一行设置两个 textInputs,命名为 安卓模拟器中的有效期和CVV。

<View style={{flex: 1, flexDirection: 'row'}}>
<Text style={styles.label}style= {{width : 100}}>Expiration date</Text>
    <View style={styles.inputWrap}>
        <TextInput style={styles.inputdate} />  
    </View>

      <Text style={styles.label}>CVV</Text>
   <View style={styles.inputWrap}>
      <TextInput  style={styles.inputcvv } maxLength={17} />
  </View>

这里包含两个 textInputs 的 CSS \

inputWrap: {
             borderColor: '#cccccc',
             borderBottomWidth: 1,
             marginBottom: 10,
   },
     inputdate: {
        fontSize: 14,
        marginBottom : -12,
        color: '#6a4595',
      },
      inputcvv: {
        fontSize: 14,
        marginBottom : -12,
        color: '#6a4595',
      },

请让我知道如何在同一行设置它..提前谢谢

【问题讨论】:

    标签: javascript android reactjs react-native react-universal


    【解决方案1】:

    使用 React Native,您需要使用 Flexbox 来布置组件。查看Flexbox docs here。

    你想做这样的事情:

    import React, { Component } from "react";
    import { Text, View, StyleSheet, TextInput } from "react-native";
    
    export default class App extends Component {
      render() {
        return (
          <View style={styles.row}>
            <View style={styles.inputWrap}>
              <Text style={styles.label}>Expiration date</Text>
              <TextInput style={styles.inputdate} />
            </View>
    
            <View style={styles.inputWrap}>
              <Text style={styles.label}>CVV</Text>
              <TextInput style={styles.inputcvv} maxLength={17} />
            </View>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      row: {
        flex: 1,
        flexDirection: "row"
      },
      inputWrap: {
        flex: 1,
        borderColor: "#cccccc",
        borderBottomWidth: 1,
        marginBottom: 10
      },
      inputdate: {
        fontSize: 14,
        marginBottom: -12,
        color: "#6a4595"
      },
      inputcvv: {
        fontSize: 14,
        marginBottom: -12,
        color: "#6a4595"
      }
    });
    

    这里的重要部分是&lt;View style={styles.row}&gt; 元素上的flexDirection: "row" 和&lt;View style={styles.inputWrap}&gt; 元素上的flex: 1。

    您可以使用 Snack (Expo) 编辑和运行此 sn-p:

    https://snack.expo.io/rySUxTKuZ

    【讨论】:

      【解决方案2】:

      如图所示划分你的整体视图。

          export default class App extends Component {
          render() {
          return (
            <View style={styles.outerContainer}>
              <View style={styles.innerContainer}>
                <Text style={styles.fieldName}>
                  Name1
                </Text>
                <View style={styles.textInputContainer}>
                  <TextInput />
                </View>
              </View>
              <View style={styles.innerContainer}>
                <Text style={styles.fieldName}>
                  Name2
                </Text>
                <View style={styles.textInputContainer}>
                  <TextInput />
                </View>
              </View>
            </View>
          );
        }
      }
      
          const styles = StyleSheet.create({
            container: {
              flex: 1,
              flexDirection: 'row',
            },
            innerContainer: {
              flex: 0.5,
              flexDirection: 'row'
            },
            fieldName: {
              flex: 1,
            },
            textInputContainer: {
              flex: 3,
            },
          });
      

      在必要时留出边距。

      【讨论】:

        【解决方案3】:

        你可以试试这样的

        render() {
        return (
          <View style={{
            flexDirection: 'row',
            alignItems: 'flex-start',
            height:100
          }}>
            <View style={styles.inputWrap}>
              <Text style={styles.label} >Expiration date</Text>
              <TextInput style={styles.inputDate} />
            </View>
        
            <View style={styles.inputWrap}>
              <Text style={styles.label}>CVV</Text>
              <TextInput style={styles.inputCvv} maxLength={17} />
            </View>
          </View>
         );
         }
        }
        
        const styles = StyleSheet.create({
          label: {
          flex: 1,
          fontWeight: 'bold'
        },
        inputWrap: {
          flex: 1,
          justifyContent: 'space-between',
          flexDirection: 'column'
        },
        inputDate: {
          flex: 1,
          backgroundColor: '#108c96',
        },
        inputCvv: {
          flex: 1,
          backgroundColor: '#6fa511',
        
        }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-16
          • 2018-06-11
          • 2017-09-15
          • 2018-12-24
          • 2018-09-18
          • 2017-09-06
          • 1970-01-01
          • 2019-12-12
          相关资源
          最近更新 更多