日历 日期时间选择器
1.日历
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <CalendarView android:layout_width="wrap_content" android:layout_height="wrap_content" android:firstDayOfWeek="2" android:maxDate="12/31/2016" android:minDate="01/01/2016" android:id="@+id/cv_1" android:background="#999" android:visibility="gone"> </CalendarView> </LinearLayout>
2.日期选择器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <DatePicker android:layout_width="match_parent" android:layout_height="match_parent" android:maxDate="12/31/2023" android:minDate="01/01/2000" android:calendarViewShown="false" android:id="@+id/dp_1" android:visibility="gone" > </DatePicker> </LinearLayout>
3.时间选择器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TimePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tp_1" > </TimePicker> </LinearLayout>
4.代码试行命令
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.administrator.testapp2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.CalendarView; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; /** * Created by Administrator on 2016/05/07. */ public class TestActivity2 extends AppCompatActivity{ CalendarView cv_1; DatePicker dp_1; TimePicker tp_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test2); cv_1 = (CalendarView)findViewById(R.id.cv_1); cv_1.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { Toast.makeText(TestActivity2.this, "选中的日期是:"+year+"-"+month+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); } }); dp_1 = (DatePicker)findViewById(R.id.dp_1); dp_1.init(2015,0,1,new DatePicker.OnDateChangedListener(){ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(TestActivity2.this, "选中的日期是:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); } }); tp_1 = (TimePicker)findViewById(R.id.tp_1); tp_1.setIs24HourView(true); tp_1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { Toast.makeText(TestActivity2.this, "选中的时间是:"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); } }); } }
5.运行代码目录
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.testapp2"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".TestActivity2"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>