0

反応ネイティブアプリを開発しています。そして、私はその中で日時ピッカーを使用しています。私の問題は、日時を選択した直後に、日時ピッカーから選択した日時を textInput に表示したいことです...その方法。これが私のコードです

import React from 'react';
import { StyleSheet, Text, View, TextInput } from 'react-native';
import DateTimePicker from 'react-native-modal-datetime-picker';

export default class App extends React.Component {
  constructor(props){
    super(props)

    this.state=({ 

      isDateTimePickerVisible: false,
      selecteddate:''
    })
  }

  _showDateTimePicker = () => this.setState({ isDateTimePickerVisible: true });

  _hideDateTimePicker = () => this.setState({ isDateTimePickerVisible: false });

  _handleDatePicked = (pickeddate) => {
    day   = pickeddate.getDate();
    month = pickeddate.getMonth();
    year  = pickeddate.getFullYear();
    console.log('A date has been picked: ' + day + '-' + month + '-' + year);
    exdate= day + '-' + month + '-' + year
    this._hideDateTimePicker();
  };

  onFocus = () => {
    this._handleDatePicked();
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <TextInput 
        placeholder="placeholder..."
        onFocus={ () => this._showDateTimePicker() }
        value={this.state.selecteddate}
        />
        {/* //--------------------------------------DateTimePicker */}
        <DateTimePicker
                      isVisible={this.state.isDateTimePickerVisible}
                      onConfirm={this._handleDatePicked}
                      onCancel={this._hideDateTimePicker}
                      mode={'date'}
                      datePickerModeAndroid={'spinner'}

                    />
            {/* //-------------------------------------- */}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
4

2 に答える 2

3

_handleDatePicked メソッドに set state を追加するのを忘れただけです!!!


 _handleDatePicked = (pickeddate) => {
            day   = pickeddate.getDate();
            month = pickeddate.getMonth();
            year  = pickeddate.getFullYear();
            console.log('A date has been picked: ' + day + '-' + month + '-' + year);
            exdate= day + '-' + month + '-' + year
            this.setState({selecteddate : day + '-' + month + '-' + year}) 
            this._hideDateTimePicker();
          };
于 2019-02-18T04:32:24.947 に答える
1

_handleDatePicked() で選択状態を設定する必要があります

_handleDatePicked = (pickeddate) => {
+    this.setState({selecteddate : day + '-' + month + '-' + year})
  }

準備万端です!

于 2019-02-18T04:40:37.137 に答える