一、代码
/**
* 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',
}
});
二、效果图
 

DatePickerAndroid用法的更多相关文章

  1. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  2. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  3. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  4. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  5. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  6. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  7. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  8. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

  9. 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)

    vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...

随机推荐

  1. 微信小程序1rpx border ios真机显示不全问题

    无意间测试发现,把border的颜色的透明度颜色改成0.99就可以了.1就不行. 边框显示不全的写法: border:1rpx solid rgba(244,84,80,1); 将边框代码的透明度改成 ...

  2. 『链接』Microsoft Visual C Redistributable/VC 再发行库 下载哪家强?

    http://www.standaloneofflineinstallers.com/2015/12/Microsoft-Visual-C-Redistributable-2015-2013-2012 ...

  3. SQL Pretty Printer for SSMS 很不错的SQL格式化插件

    写SQL语句或者脚本时,看到凌乱的格式就头大了,于是决心找一款SQL语句格式化的工具. 功夫不负有心人还真的被我找到一款很好用,很方便的SQL Server插件:SQL Pretty Printer ...

  4. Varnish http缓存服务器

    http://blog.51cto.com/hexiaoshuai/1909183 https://jefferywang.gitbooks.io/varnish_4_1_doc_zh/content ...

  5. 微信小程序wx.previewImage实用案例(交流QQ群:604788754)

    本案例是可以滑动预览多张图片效果.(本案例在本地配置好之后,请扫描二维码到手机滑动预览.在开发者工具上预览,滑动不是很流畅) 图片必须选择远程图片,本地图片无法实现预览. 或是通过wx.chooseI ...

  6. Win10系列:C#应用控件进阶6

    路径 路径(Path)可以用来定义任意形状的曲线和几何图形,当然这种任意性也带来了复杂性.为了方便的绘制几何图形,微软在Visual Studio 2012安装包中为程序开发者提供了免费的Blend ...

  7. java----判断闰年和平年

    public class year{ public static void main(String[] args){ int year=2010; if((year%4==0&&yea ...

  8. 数据类型、运算符及Scanner类练习

    数字加密.要求输入一个四位的正整数,每位数字加5再除以10取余,并替换该数字,再千位数与个位数互换,十位数与百位数互换. import java.util.Scanner;/** * 加密数字问题 * ...

  9. Python_Mix*内置函数

    数学运算(7个) abs()求数值的绝对值 divmod()返回两个数值的商和余数 max()返回可迭代对象中的元素中的最大值或者所有参数的最大值 min()返回可迭代对象中的元素中的最小值或者所有参 ...

  10. selenium操作日历控件

    日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用js去掉 ...