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. as3 程序域

    问题我要在应用程序中载入其他域的swf文件,并且允许它访问程序中的 ActionScript 解决办法使用flash.system.Security.allowDomain( ), flash.sys ...

  2. ABAP-SAP的LUW和DB的LUW的区别

    转载:http://www.cnblogs.com/helileng/archive/2010/10/14/1851409.html LUW是Logical Unit of Work,也就是逻辑工作单 ...

  3. ios push local notification

    UILocalNotification* localNotification = [[UILocalNotification alloc]init]; localNotification.alertB ...

  4. Linux下生成openssl自签名证书

    校验证书是否被 CA 证书签名,正确的情况: $ openssl verify -CAfile /etc/kubernetes/cert/ca.pem /etc/kubernetes/cert/kub ...

  5. binlog、redo log、undo log区别

    root@(none) 04:17:18>show variables like 'innodb_log_group_home_dir';+--------------------------- ...

  6. git命令图片

  7. TEXT 15 A text a day...

    TEXT 15 A text a day... Mar 24th 2006 From The Economist print edition The medical uses of mobile ph ...

  8. TEXT 8 Ready, fire, aim

    TEXT 8 Ready, fire, aim 预备!开火!瞄准!! Feb 16th 2006 From The Economist print edition Foreword:A vice-pr ...

  9. python判断unicode是否是汉字,数字,英文,或者其他字符

    下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...

  10. DataType 数据类型

    基本类型:四类八种:数值 : 整数:byte,short,int,long.默认是 int 小数:float,double                  默认是 double 布尔:boolean ...