简体   繁体   English

如何在react native中设置同一行的两个输入?

[英]How to set Two inputs on same row in react native ?

Hey I want to set two textInputs on same line , named Expiration date and CVV in android simulator. 嘿我想在同一行设置两个textInputs,在android模拟器中命名为Expiration date和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>

Here it is including CSS for both textInputs \\ 这里包括两个textInputs的CSS

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

Please let me know how can i set this on same line.. thanks in advance 请让我知道如何在同一行设置此... ..提前感谢

With React Native you need to use Flexbox for laying out your components. 使用React Native,您需要使用Flexbox来布局组件。 Check out the Flexbox docs here . 在这里查看Flexbox文档

You want to do something like this: 你想做这样的事情:

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"
  }
});

The important part here is the flexDirection: "row" on the <View style={styles.row}> element and the flex: 1 on the <View style={styles.inputWrap}> elements. 这里最重要的部分是flexDirection: "row"<View style={styles.row}>元件和flex: 1<View style={styles.inputWrap}>元素。

You can edit and run this snippet with Snack (Expo): 您可以使用Snack(Expo)编辑和运行此代码段:

https://snack.expo.io/rySUxTKuZ https://snack.expo.io/rySUxTKuZ

在此输入图像描述

you can try something like this 你可以尝试这样的事情

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',

}
});

UI分手

Divide your overall view as shown in figure. 划分整体视图,如图所示。

    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,
      },
    });

Give margins wherever necessary. 在必要时给予保证金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM