1. MainActivity.java  
  2.     
  3. public class MainActivity extends Activity  
  4. {  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         Button dateBn = (Button)findViewById(R.id.dateBn);  
  11.         Button timeBn = (Button)findViewById(R.id.timeBn);  
  12.         //为"设置日期"按钮绑定监听器  
  13.         dateBn.setOnClickListener(new OnClickListener()  
  14.         {  
  15.             @Override  
  16.             public void onClick(View source)  
  17.             {  
  18.                 Calendar c = Calendar.getInstance();  
  19.                 // 直接创建一个DatePickerDialog对话框实例,并将它显示出来  
  20.                 new DatePickerDialog(MainActivity.this,  
  21.                     // 绑定监听器  
  22.                     new DatePickerDialog.OnDateSetListener()  
  23.                     {  
  24.                         @Override  
  25.                         public void onDateSet(DatePicker dp, int year,  
  26.                             int month, int dayOfMonth)  
  27.                         {  
  28.                             EditText show = (EditText) findViewById(R.id.show);  
  29.                             show.setText("您选择了:" + year + "年" + (month + 1)  
  30.                                 + "月" + dayOfMonth + "日");  
  31.                         }  
  32.                     }  
  33.                     //设置初始日期  
  34.                     , c.get(Calendar.YEAR)  
  35.                     , c.get(Calendar.MONTH)  
  36.                     , c.get(Calendar.DAY_OF_MONTH)).show();  
  37.             }  
  38.         });  
  39.         //为"设置时间"按钮绑定监听器  
  40.         timeBn.setOnClickListener(new OnClickListener()  
  41.         {  
  42.             @Override  
  43.             public void onClick(View source)  
  44.             {  
  45.                 Calendar c = Calendar.getInstance();  
  46.                 // 创建一个TimePickerDialog实例,并把它显示出来  
  47.                 new TimePickerDialog(MainActivity.this,  
  48.                     // 绑定监听器  
  49.                     new TimePickerDialog.OnTimeSetListener()  
  50.                     {  
  51.                         @Override  
  52.                         public void onTimeSet(TimePicker tp, int hourOfDay,  
  53.                             int minute)  
  54.                         {  
  55.                             EditText show = (EditText) findViewById(R.id.show);  
  56.                             show.setText("您选择了:" + hourOfDay + "时"  
  57.                                 + minute + "分");  
  58.                         }  
  59.                     }  
  60.                     //设置初始时间  
  61.                     , c.get(Calendar.HOUR_OF_DAY)  
  62.                     , c.get(Calendar.MINUTE)  
  63. 小时制  
  64.                     , true).show();  
  65.             }  
  66.         });  
  67.     }  
  68. }  
  69.     
  70. XML文件  
  71.     
  72. <?xml version="1.0" encoding="utf-8"?>  
  73. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  74.     android:orientation="vertical"  
  75.     android:layout_width="match_parent"  
  76.     android:layout_height="match_parent"  
  77.     android:gravity="center">  
  78. <EditText   
  79.     android:id="@+id/show"  
  80.     android:layout_width="match_parent"   
  81.     android:layout_height="wrap_content"   
  82.     android:editable="false"  
  83.     />  
  84. <LinearLayout   
  85.     android:orientation="horizontal"  
  86.     android:layout_width="match_parent"  
  87.     android:layout_height="wrap_content"  
  88.     android:gravity="center"  
  89.     >  
  90. <Button  
  91.     android:id="@+id/dateBn"  
  92.     android:layout_width="wrap_content"  
  93.     android:layout_height="wrap_content"  
  94.     android:text="设置日期"  
  95.     />  
  96. <Button  
  97.     android:id="@+id/timeBn"  
  98.     android:layout_width="wrap_content"  
  99.     android:layout_height="wrap_content"  
  100.     android:text="设置时间"  
  101.     />     
  102. </LinearLayout>  
  103. </LinearLayout>  

效果

DatePickerDialog TimePickerDialog的更多相关文章

  1. android 开发DatePickerDialog/TimePickerDialog对话框的实现

    AndroidAPI提供了Dialog对话框控件,DatePickerDialog/TimePickerDialog均是AlertDialog的子类,通过DatePickerDialog/TimePi ...

  2. Android DatePickerDialog TimepickerDialog

    package com.example.myact5; import java.util.Calendar; import android.app.DatePickerDialog; import a ...

  3. 2.5.5 使用DatePickerDialog, TimePickerDialog

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  4. android入门 — ProgressDialog/DatePickerDialog/TimePickerDialog

    这三个Dialog都是AlertDialog的子类. ①DatePickerDialog 1.创建DatePickerDialog的实例: 2.通过Calendar类获得系统时间: 3.通过DateP ...

  5. android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow

    ProgressDialog(精度条对话框): 1.直接调用ProgressDialog提供的静态方法show()显示 2.创建ProgressDialog,再设置对话框的参数,最后show()出来 ...

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

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

  7. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  8. android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)

    DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...

  9. 完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog

    完全参照系统自带的DatePickerDialog.TimePickerDialog的源代码仿写的DateTimePickerDialog.具有同时选择日期.时间的功能.在2.2.2.3平台,显示的效 ...

随机推荐

  1. linux中与Oracle有关的内核参数详解

    工作当中遇到oracle运行时CPU占用率达到90%以上,调小以下参数值后恢复正常. fs.file-max = 65536 net.core.rmem_default=262144 net.core ...

  2. Haskell语言学习笔记(49)ByteString Text

    Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ...

  3. 设计模式学习笔记(1)Iterator

    Iterator 模式 public interface Iterator { public boolean hasNext(); public Object next(); } public int ...

  4. 在Eclipes中查看源代码和大纲快速定位

    1 在Eclipes中查看源代码,快捷键使用clrl+光标,选择你要查看的方法和属性查看源代码.例如你想看StringBuilder这个类源代码 StringBuilder allow = new S ...

  5. Delphi笔记-自定义提示窗口

    unit pbHint; interface uses Windows, Controls, Forms, Graphics; type TPBHint=class(THintWindow) //要自 ...

  6. SWFUpload乱码问题的解决

    目前比较流行的是使用SWFUpload控件,这个控件的详细介绍可以参见官网http://demo.swfupload.org/v220/index.htm 在使用这个控件批量上传文件时发现中文文件名都 ...

  7. 通过Github Pages在线查看百度前端技术学院完成的任务成果

    前言 .note-content {font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHe ...

  8. SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门

    SpringMVC在使用过程中,大多是使用注解,对它的实现接口之类的关系理解变得模糊, 通过对XML配置的理解,可以理清各个类的关系,譬如控制器类要实现Controller接口. 接触SpringMV ...

  9. 第六章 图(b1)邻接矩阵

  10. MyBatis核心配置文件详解

    ------------------------siwuxie095                                     MyBatis 核心配置文件详解         1.核心 ...