项目步骤:

1.首先在Main.xml布局文件中添加一个Button标签,用来点击显示日期控件,Main.xml内容如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity" >

<Button

android:id="@+id/showDatePickerButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="点击显示日期控件"/>

</RelativeLayout>

2.在Activity中声明并初始化日期控件,并为Button设置监听器,Activity内容如下:

public class MainActivity extends Activity {

private Button showDatePickerButton = null;

//该常量用于标识DatePickerDialog

private static final int DATE_PICKER_ID = 1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//设置布局文件

setContentView(R.layout.activity_main);

//根据id找到布局文件中对应的组件

showDatePickerButton = (Button)findViewById(R.id.showDatePickerButton);

//为按钮设置监听器

showDatePickerButton.setOnClickListener(new ButtonListener());

}

class ButtonListener implements android.view.View.OnClickListener{

@Override

public void onClick(View v) {

//此方法用于显示DatePickerDialog

showDialog(DATE_PICKER_ID);

}

}

//声明一个监听器,使用匿名内部类

//监听器,显示年月日

DatePickerDialog.OnDateSetListener onDateSetListener  = new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int monthOfYear,     int dayOfMonth) {

System.out.println(year+"-"+monthOfYear+"-"+dayOfMonth);

}

};

/**   * 初始化日期   */

@Override

protected Dialog onCreateDialog(int id) {

switch(id){

//初始化日期

case DATE_PICKER_ID:   return new DatePickerDialog(this, onDateSetListener, 2013, 07, 20);

}

return null;

}

}

【基础篇】DatePickerDialog日期控件的基本使用(一)的更多相关文章

  1. 【基础篇】DatePickerDialog日期控件的基本使用(二) ——分别获取年、月、日、时、分

    项目步骤: 1.在Main.xml布局文件中定义对应的组件,Main.xml内容如下: <?xml version="1.0" encoding="utf-8&qu ...

  2. iOS开发基础篇-手写控件

    一.手写控件的步骤 1)使用相应的控件类创建控件对象: 2)设置该控件的各种属性: 3)添加空间到视图中: 4)如果是 UIButton 等控件,还需考虑控件的单击事件等: 二.添加 UIButton ...

  3. cocos2dx基础篇(10) 按钮控件CCControlButton

    [3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)按钮事件回调依旧为 cccontrol_selector ,没有使用 CC_CALLBACK_2 (4)按钮状态  ...

  4. cocos2dx基础篇(9) 滑块控件CCControlSlider

    [3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)CCControlEvent 改为强枚举 Control::EventType (4)CCControlEvent ...

  5. jQuery Datepicker日期控件

    datepicker可以为bootstrap添加一个事件选择控件,适用于任何需要调用的场合,支持多种事件格式输出(比如:dd, d, mm, m, yyyy, yy等),是制作网页不可缺失的插件. R ...

  6. bootstrap-datetimepicker 日期控件的开始日期

    今天做日期控件,需求要求设置一个时间范围限制,选择从今天开始的日期才可以选择,今天以前都不可以选择 主要体现在bootstrap-datetimepicker控件下面的2个日期参数 weekStart ...

  7. WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 日历控 ...

  8. [liferay6.2]input-date日期控件

    input-date日期控件 liferay6.2中默认提供了一个简单的日期控件input-date,调用代码片段如下: <% Calendar calendar = Calendar.getI ...

  9. selenium 处理日期控件

    今天遇到日期控件无法处理的问题,在北京-air的帮助下,看了下这篇blog http://www.cnblogs.com/Fskjb/archive/2011/10/27/2227111.html 根 ...

随机推荐

  1. Generic type test java

    package test; public class GenericTest { public class Room<T> { private T t; public void add(T ...

  2. <Sicily> 生成字符串

    一.题目描述 假设一个字符串只由字符'0','1','?'组成,其中字符'?'表示该字符可由字符'0'或'1'替代. 现有一些字符串,根据这些字符串生成所有可生成的字符串. 如:{10,?1,0? } ...

  3. bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  4. CF981C(菊花图)

    题目描述 RAMESS知道很多关于树的问题(无循环的无向连通图)! 他创建了一个新的有用的树的划分,但他不知道如何构造它,所以他请求你的帮助! 划分是从树上的边中分裂出一些简单的路径,使得每个两条路径 ...

  5. [HNOI2004]高精度开根

    题目:洛谷P2293.BZOJ1213. 题目大意:给你$n,k(n\leq 10^{10000},k\leq 50)$,求$\lfloor \sqrt[k]{n}\rfloor$. 解题思路:高精度 ...

  6. Python 读写文件 小应用:生成随机的测验试卷文件

    去年学习了python的读写文件部分,了解了python读写的常用模块os.shelve,今天准备把课后作业试着自己做一下 目标:1)生成35份试卷.每个试卷有50道选择题 2)为了防止有学生作弊,需 ...

  7. Unity C# 设计模式(三)工厂方法模式

    定义: 定义一个创建对象的接口(父类),由子类决定需要实例化哪一个类. 这样,核心工厂类成为了一个抽象角色,不再负责产品的创建,仅提供具体工厂类所必须实现的接口,这样进一步抽象化的好处是使得工厂方法模 ...

  8. ArcGIS api for javascript——地图配置-定制缩放动画

    描述 本例展示了当用户放大或缩小地图时如何定义地图的动画.zoomDuration和zoomRate是Dojo动画属性,他们确定了动画的duration和帧刷新的rate .这些属性单位是毫秒,zoo ...

  9. [Python] Boolean Or "Mask" Index Arrays filter with numpy

    NumPy Reference: Indexing Integer array indexing Boolean array indexing Note: The expression a < ...

  10. F - Humidex(1.4.2)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...