如何在 React Native 的同一行设置两个输入?

新手上路,请多包涵

嘿,我想在同一行设置两个 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>

这里包括两个 textInputs 的 CSS

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

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

原文由 Madhur 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 691
2 个回答

使用 React Native,你需要使用 Flexbox 来布置你的组件。 在此处查看 Flexbox 文档

你想做这样的事情:

 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.

您可以使用 Snack (Expo) 编辑和运行此代码段:

https://snack.expo.io/rySUxTKuZ

原文由 jävi 发布,翻译遵循 CC BY-SA 3.0 许可协议

用户界面分解

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

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

必要时给予边距。

原文由 Ashish Dulhani 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题