activity_main.xml:

   <Button
android:id="@+id/showDatePickerButton"
android:text="@string/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

MainActivity.java:

package leihu.date_and_time;

import android.os.Bundle;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.DatePicker;

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);

                   showDatePickerButton = (Button)findViewById

(R.id.showDatePickerButton);

                   showDatePickerButton.setOnClickListener(new ButtonListener());

         }

         private class ButtonListener implements OnClickListener{

                   @Override

                   public void onClick(View v) {

                            // TODO Auto-generated method stub

                            //此方法用于显示DatePickerDialog

                            showDialog(DATE_PICKER_ID);

                   }

         }

         DatePickerDialog.OnDateSetListener onDateSetListener = new

DatePickerDialog.OnDateSetListener() {

                   @Override

                   public void onDateSet(DatePicker view, int year, int monthOfYear,

                                     int dayOfMonth) {

                            // TODO Auto-generated method stub

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

                   }

         };

         protected Dialog onCreateDialog(int id){

                   switch(id){

                   case DATE_PICKER_ID:

                            return new DatePickerDialog

(this,onDateSetListener,2012,11,21);

                   }
return null;
} }

  

 

DatePicker和DatePickerDialog的使用的更多相关文章

  1. Android--UI之DatePicker、TimePicker...

    前言 这一篇博客分别讲解Android平台下,关于日期和时间的几个相关控件.包括DatePicker(日期选择控件).TimePicker(时间选择控件).DatePickerDialog(日期选择对 ...

  2. DatePicker和DataPickerDialog以及AutoCompleteTextView的基本使用方法

    1.DatePicker和DatePickerDialog的基本使用方法: main.xml: <?xml version="1.0" encoding="utf- ...

  3. android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)

    第三节(2):常用控件之ViewPager.日期时间相关.ListView  一.ViewPager 实例:结合PagerAdapter滑动切换图片  二.日期时间相关:AnalogClock\Dig ...

  4. Android精通教程V

    前言 大家好,给大家带来Android精通教程V的概述,希望你们喜欢 前言 如果你想学习Android开发,那你就要了解Java编程,这是基础,也是重点,如果没学Java语法就先学习,再来学Andro ...

  5. Android开发中常用Dialog(普通弹窗&时间选择器&日历选择器)

    引言 开发中,我们会有很多地方使用 Dialog 来展示一些提示信息或设置信息.如:用户提示.进度展示.时间设置.日期设置等. 下面我和大家一些学习下Android中常用的几种Dialog吧~ * 首 ...

  6. Android中DatePicker与TimePicker用法讲解(包括DatePickerDialog与TimePickerDialog)

    实现效果:将DatePicker和TimePicker修改的日期和时间实时显示在程序标题栏上. 1.通过DatePicker和TimePicker来实现 布局为main.xml <?xml ve ...

  7. Xamarin中使用DatePickerDialog的相关问题

    在Xamarin中在使用Datepicker的时候,一般情况下只需要在对应的按钮或其他控件的点击事件中使用如下语句即可完成: EditText etBirthday = FindViewById< ...

  8. 【Android自学日记】使用DatePicker以及TimePicker显示当前日期和时间

    DatePicker 1.获取一个日历对象: Calendar cal=Calendar.getInstance(); 2.获取当前日期及时间: int year=cal.get(Calendar.Y ...

  9. DatePickerDialog、AutoCompleteTextView

    DatePickerDialog选择日期,调用showDialog(int id)方法,会执行onCreateDialog方法: @Override protected Dialog onCreate ...

随机推荐

  1. Plastic Sprayers Manufacturer - Ingenious Design Of Spray Plastic Bottle

    Plastic bottles are now an indispensable container in life. Plastic bottles will appear in all aspec ...

  2. 浏览器 User-Agent 整理

    也可以去这里查询:http://tools.jb51.net/table/useragent window.navigator.userAgent 1) Chrome Win7: Mozilla/5. ...

  3. Shell编程WEB界面展示实践

    操作系统:win7 虚拟机:Virtual box with Ubuntu13.10 WEB服务器: Nginx WEB服务器发布目录:/usr/local/nginx/html/c 测试文件:lis ...

  4. web.xml中的welcome-file-list标签作用

    welcome-file-list是一个配置在web.xml中的一个欢迎页,用于当用户在url中输入项目名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面. ...

  5. 360网安学习笔记——Web安全原理与实践

    网络安全 基本技能: 1.编程语言 2.计算机网络 3.操作系统 4.office 专业技能 1.web安全 2.网络安全 3.渗透测试 4.代码审计 能力提升 1.书籍 2.站点 3.安全平台 We ...

  6. leetcode网解题心得——61. 旋转链表

    目录 leetcode网解题心得--61. 旋转链表 1.题目描述 2.算法分析: 3.用自然语言描述该算法 4.java语言实现 5.C语言实现 leetcode网解题心得--61. 旋转链表 1. ...

  7. Python学习笔记010

    倒三角 num2 = int(input("Line:")) while num2 > 0:    num1 = num2    while num1 > 0:     ...

  8. Nginx平滑升级版本!(重点)

    一.解释nginx的平滑升级 随着nginx越来越流行使用,并且nginx的优势也越来越明显,nginx的版本迭代也开始了加速模式,1.9.0版本的nginx更新了许多新功能,例如stream四层代理 ...

  9. Es查询结果集默认是10000,更新设置

    Es查询结果集默认是10000,结果集大小是int,最大为21亿左右 PUT _all/_settings?preserve_existing=true { "index.max_resul ...

  10. js面试必考:this

    this是前端面试中必考的基础知识点,也是新手小白在做项目中经常晕头转向的问题.但其实this不难理解. 判断this指向时,记住以下几点: 判断函数类型, 1.1 如果是箭头函数,则为第一个包裹箭头 ...