DatePicker(日历选择器)和TimePicker

MainActivity
1
package ldatedemo.example.administrator.datepickerdemo; 2 3 import android.app.ActionBar; 4 import android.app.Activity; 5 import android.app.DatePickerDialog; 6 import android.app.TimePickerDialog; 7 import android.os.Bundle; 8 import android.support.v7.app.ActionBarActivity; 9 import android.support.v7.app.AppCompatActivity; 10 import android.widget.DatePicker; 11 import android.widget.TimePicker; 12 13 import java.sql.Date; 14 import java.util.Calendar; 15 16 public class MainActivity extends AppCompatActivity { /*要继承AppCompatActivity才有标题显示,继承Activity无标题显示*/ 17 18 private TimePicker timePicker; 19 private DatePicker datePicker; 20 private Calendar calendar; 21 private int day; 22 private int year; 23 private int month; 24 private int hour; 25 private int minute; 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 calendar=Calendar.getInstance();/*获取日历对象*/ 31 year=calendar.get(Calendar.YEAR); 32 month=calendar.get(Calendar.MONTH)+1;/*Calendar计算月份从0开始*/ 33 day=calendar.get(Calendar.DAY_OF_MONTH); 34 hour=calendar.get(Calendar.HOUR_OF_DAY); 35 minute=calendar.get(Calendar.MINUTE); 36 setTitle(year+"-"+month+"-"+day+"-"+hour+"-"+minute); 37 datePicker= (DatePicker) findViewById(R.id.datePicker); 38 timePicker= (TimePicker) findViewById(R.id.timePicker); 39 /*以贴合屏幕方式*/ 40 datePicker.init(year,calendar.get(Calendar.MONTH),day, new DatePicker.OnDateChangedListener() { /*datePicker初始化*/ 41 @Override /*用get(Calendar.MONTH)已经加过一了,new DatePicker.OnDateChangedListener()监听器*/ 42 public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 43 setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); 44 } 45 });/*datePicker初始化*/ 46 /* 以贴合屏幕方式*/ 47 timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { 48 @Override /*OnTimeChangedListener()监听器*/ 49 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 50 setTitle(hourOfDay+":"+minute); 51 } 52 }); 53 /*以对话框的形式*/ 54 new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 55 @Override 56 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 57 setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); 58 } 59 },year, calendar.get(Calendar.MONTH),day).show(); 60 new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 61 @Override 62 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 63 setTitle(hourOfDay+":"+minute); 64 } 65 66 },hour,minute,true).show(); 67 } 68 }

 

layout
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1"> <CalendarView android:layout_width="match_parent" android:layout_height="181dp" android:id="@+id/calendarView" /> <TimePicker android:layout_width="344dp" android:layout_height="152dp" android:id="@+id/timePicker" /> <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datePicker" />

DatePicker(日历选择器)和TimePicker_第1张图片

转载于:https://www.cnblogs.com/633sylss/p/5459042.html

你可能感兴趣的:(移动开发,java)