日历 日期时间选择器

1.日历

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="vertical"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6 <CalendarView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:firstDayOfWeek="2"
10         android:maxDate="12/31/2016"
11         android:minDate="01/01/2016"
12         android:id="@+id/cv_1"
13         android:background="#999"
14         android:visibility="gone">
15     </CalendarView>
16 
17 </LinearLayout>

2.日期选择器

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="vertical"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6 <DatePicker
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9         android:maxDate="12/31/2023"
10         android:minDate="01/01/2000"
11         android:calendarViewShown="false"
12         android:id="@+id/dp_1"
13         android:visibility="gone"
14         >
15     </DatePicker>
16 
17 </LinearLayout>

3.时间选择器

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="vertical"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6 <TimePicker
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:id="@+id/tp_1"
10         >
11     </TimePicker>
12 </LinearLayout>

4.代码试行命令

 1 package com.example.administrator.testapp2;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.widget.CalendarView;
 6 import android.widget.DatePicker;
 7 import android.widget.TimePicker;
 8 import android.widget.Toast;
 9 
10 /**
11  * Created by Administrator on 2016/05/07.
12  */
13 public class TestActivity2 extends AppCompatActivity{
14 
15     CalendarView cv_1;
16     DatePicker dp_1;
17     TimePicker tp_1;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_test2);
23 
24         cv_1 = (CalendarView)findViewById(R.id.cv_1);
25 
26         cv_1.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
27             @Override
28             public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
29 
30                 Toast.makeText(TestActivity2.this, "选中的日期是:"+year+"-"+month+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
31             }
32         });
33 
34         dp_1 = (DatePicker)findViewById(R.id.dp_1);
35 
36         dp_1.init(2015,0,1,new DatePicker.OnDateChangedListener(){
37 
38             @Override
39             public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
40 
41                 Toast.makeText(TestActivity2.this, "选中的日期是:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
42             }
43         });
44 
45         tp_1 = (TimePicker)findViewById(R.id.tp_1);
46 
47         tp_1.setIs24HourView(true);
48 
49         tp_1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
50             @Override
51             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
52 
53                 Toast.makeText(TestActivity2.this, "选中的时间是:"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
54             }
55         });
56     }
57 
58 
59 
60 }

5.运行代码目录

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.testapp2">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <activity android:name=".TestActivity2">
12             <intent-filter>
13             <action android:name="android.intent.action.MAIN" />
14             <category android:name="android.intent.category.LAUNCHER" />
15         </intent-filter>
16         </activity>
17 
18     </application>
19 
20 </manifest>
原文地址:https://www.cnblogs.com/TENOKAWA/p/5471190.html