zoukankan      html  css  js  c++  java
  • DatePickerAndroid用法

    一、代码
    /** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; import React, {Component} from 'react'; import{ StyleSheet, Text, View, DatePickerAndroid, TouchableHighlight, } from 'react-native'; //简单封装一个组件 class CustomButton extends Component { render() { return ( <TouchableHighlight style={styles.button} underlayColor="#a5a5a5" onPress={this.props.onPress}> <Text style={styles.buttonText}>{this.props.text}</Text> </TouchableHighlight> ); } } export default class App extends Component { constructor(props){ super(props); this.state={ presetDate: new Date(2016, 3, 5), allDate: new Date(2020, 4, 5), simpleText: '选择日期,默认今天', minText: '选择日期,不能比今日再早', maxText: '选择日期,不能比今日再晚', presetText: '选择日期,指定2016/3/5', }; } formateDate(date) { var year = date.getFullYear() ; var month = date.getMonth() +1 ; var day = date.getDate() ; var formatedStr = year + '-' + month +'-' + day ; // console.log('formatedStr: ' + formatedStr ) ; return formatedStr ; } //进行创建时间日期选择器 async showPicker(stateKey, options) { try { var newState = {}; const {action, year, month, day} = await DatePickerAndroid.open(options); if (action === DatePickerAndroid.dismissedAction) { newState[stateKey + 'Text'] = 'dismissed'; } else { var date = new Date(year, month, day); newState[stateKey + 'Text'] = this.formateDate(date); newState[stateKey + 'Date'] = date; } this.setState(newState); } catch ({code, message}) { console.warn(`Error in example '${stateKey}': `, message); } } render() { return ( <View> <Text style={styles.welcome}> 日期选择器组件实例 </Text> <TouchableHighlight style={styles.button} underlayColor="#a5a5a5" onPress={this.showPicker.bind(this, 'simple',{date: this.state.simpleDate})}> <Text style={styles.buttonText}>点击弹出基本日期选择器</Text> </TouchableHighlight> <CustomButton text={this.state.presetText} onPress={this.showPicker.bind(this, 'preset', {date: this.state.presetDate})}/> <CustomButton text={this.state.minText} onPress={this.showPicker.bind(this, 'min', {date: this.state.minDate,minDate:new Date()})}/> <CustomButton text={this.state.maxText} onPress={this.showPicker.bind(this, 'max', {date: this.state.maxDate,maxDate:new Date()})}/> </View> ); } } const styles = StyleSheet.create({ welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, button: { margin:5, backgroundColor: 'white', padding: 15, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: '#cdcdcd', } });
    二、效果图
     
    点关注各类It学习资源免费送+V 1153553800
  • 相关阅读:
    (转)SGI STL中list的sort函数实现
    (转)OpenCv与Qt的结合,几种方法的比较
    (转)typeid详解
    转: C++藏书阁
    (转)Qt多线程编程
    (转)C/C++ 各种计时函数总结
    【转载】深入 Facebook 消息应用服务器
    ubuntu LAMP本地环境配置
    视频: 英语口音纠正课程
    【转载】安装 JDK1.6 / java 1.6 (linux, ubuntu, windows)
  • 原文地址:https://www.cnblogs.com/binary1010/p/8425503.html
Copyright © 2011-2022 走看看