WenJieWangFlyToWorld

导航

DatePickerDialog TimePickerDialog

  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.                     //true表示采用24小时制  
  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>  

 

效果

 

posted on 2017-06-01 20:12  WenjieWangFlyToWorld  阅读(194)  评论(0编辑  收藏  举报