简体   繁体   English

在 React Native 中触摸 TextInput 时打开键盘和/或 DatePickerIOS

[英]Open Keyboard and/or DatePickerIOS when TextInput touched in React Native

I am new to React Native and am simply looking to have a DatePickerIOS component slide up from the bottom of the screen when a TextInput is touched.我是 React Native 的新手,只是希望在触摸 TextInput 时让 DatePickerIOS 组件从屏幕底部向上滑动。

Can someone please point me in the right direction or give me a simple example?有人可以指出我正确的方向或给我一个简单的例子吗?

Below is my component.下面是我的组件。 It is very simple.这很简单。 I'm trying to get either the keyboard or the DatePickerIOS to open once the user touches the TextInput.我试图让键盘或 DatePickerIOS 在用户触摸 TextInput 后​​打开。

import React, {
  AppRegistry,
  Component,
  StyleSheet,
  Text,
  TextInput,
  View,
  DatePickerIOS
} from 'react-native';

class AddChildVisit extends Component {

render() {
  return (
    <View>
      <Text>Visit Date</Text>
      <TextInput
        style={{height: 40, width: 300, padding: 4, borderColor: 'gray', borderWidth: 1}}
        keyboardType= 'numeric'
      />
      <DatePickerIOS
        date={new Date()}
        mode="date"
      />
    </View>
  );
 }


}


module.exports = AddChildVisit;

I am using DatePickerIOS, here is example how would I do it:我正在使用 DatePickerIOS,这是我将如何做的示例:

import React, {
    AppRegistry,
    Component,
    StyleSheet,
    Text,
    TouchableOpacity,
    TextInput,
    View,
    DatePickerIOS
} from "react-native";

var moment = require('moment');

class AddChildVisit extends Component {
    constructor(props) {
        super(props);
        this.state = {
            date: new Date(),
            showDatePicker: false 
        }
    }

    render() {
        var showDatePicker = this.state.showDatePicker ?
            <DatePickerIOS
                style={{ height: 150 }}
                date={this.state.date} onDateChange={(date)=>this.setState({date})}
                mode="date"/> : <View />
        return (
            <View>
                <Text>Visit Date</Text>
                <TouchableOpacity style={{height: 40, width: 300, padding: 4, borderColor: 'gray', borderWidth: 1}}
                                  onPress={() => this.setState({showDatePicker: !this.state.showDatePicker})}>
                    <Text>{moment(this.state.date).format('DD/MM/YYYY')}</Text>
                </TouchableOpacity>
                {showDatePicker}
            </View>
        );
    }


}

module.exports = AddChildVisit;

When you press TouchableOpacity, datepicker will show because you trigger flag to show it, and if you press it again, it will close.当您按下 TouchableOpacity 时,日期选择器会显示,因为您触发了标志来显示它,如果您再次按下它,它将关闭。 I also use moment to print date because I was getting error that I can't put object in text component etc. For this example to work you need to install moment, you can do it like this: npm install moment --save , I already used it in code example above, you can see var moment = require('moment') , and again in printing date in text {moment(this.state.date).format('DD/MM/YYYY')} .我还使用 moment 来打印日期,因为我收到了无法将对象放入文本组件等的错误。对于这个例子,你需要安装 moment,你可以这样做: npm install moment --save ,我已经在上面的代码示例中使用过,您可以看到var moment = require('moment') ,并再次在文本{moment(this.state.date).format('DD/MM/YYYY')}中打印日期。 If you don't want to install/use it, just remove lines with moment from my example - but your date won't be printed then.如果您不想安装/使用它,只需从我的示例中删除带有 moment 的行 - 但那时不会打印您的日期。 I'm not really sure how would I call date picker with TextInput because it has to trigger keyboard, and when I have date picker I don't really need keyboard.我不确定如何使用 TextInput 调用日期选择器,因为它必须触发键盘,而当我有日期选择器时,我真的不需要键盘。 I hope it helps :)我希望它有帮助:)

另一种方法是使用包 react-native-datepicker 并且适用于 Android/iOS 两个平台https://www.npmjs.com/package/react-native-datepicker

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

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