DatePicker 1.month在机器中运行时是从0开始的,在sdk方法中使用一律采用不加1的方式,只有当用于显示时才手动加1; 2.获取日期实例并初始化并打印: Calendar cal=Calendar.getInstance(); 3.获取当前日期及时间: int year=cal.get(Calendar.YEAR); int month=cal.get(Calendar.MONTH)+1; int day=cal.get(Calendar.DAY_OF_MONTH); int hour=cal.get(Calendar.HOUR_OF_DAY); int minute=cal.get(Calendar.MINUTE_OF_HOUR); setTitle(year+"-"+month+"-"+day+"-"+hour+":"+minute); 4.//使标题上的日期能随着选择的改变而改变 datePicker.init(year,month,day,new OnDateChangedListener()){ @Override public void onDateChanged(DatePicker view,int year,int cal.get(Calendar.MONTH_OF_YEAR),int dayOfMonth){ setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } }
时间选择器就比上次的日期选择器要简单很多了,首先不需要月份的换算,然后也不需要初始化,而是直接设置监听器。 timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) { setTitle(hourOfDay+":"+minute); } });
DatePickerDialog总结
1.实例化 public DatePickerDialog(Context context, //上下文
OnDateSetListener callBack, //回掉函数
int year, //初始年月日
int monthOfYear,
int dayOfMonth)
2.调用.show()方法显示
1 new DatePickerDialog(this, new OnDateSetListener() {
2
3 @Override
4 public void onDateSet(DatePicker view, int year, int monthOfYear,
5 int dayOfMonth) {
6 // TODO Auto-generated method stub
7 setTitle(year + "-" + (monthOfYear+1)+"-"+dayOfMonth );
8 }
9 }, year, month, year).show();
TimePickerDialog总结
1.实例化 public TimePickerDialog(Context context, //上下文
OnTimeSetListener callBack, //回掉函数
int hourOfDay, int minute, //初始年月日,
boolean is24HourView) //是否24小时制
1 new TimePickerDialog(this,new OnTimeSetListener() {
2
3 @Override
4 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
5 // TODO Auto-generated method stub
6
7 }
8 }, hour, minute, true).show();
ps:
DatePicker对象是以init这个方法初始化来指定DatePicker初始化的年月日以及OnDateChangedListener来监听日期的改变,
而TimePicker不需要init初始化,只需要设置监听OnTimeChangedListener来监听时间的改变,
DatePickerDailog是以对话框的方式弹出日期选择器,需要用new的方式创建并且传入对应的监听OnDateSetListener,日期初始化数据,最后记得设置对话框显示(show)方法。
同样TimePickerDailog也是需要以new的方式进行初始化,并且设置OnTimeListene
需要注意的是DatePicker的初始化月份可能需要-1的操作(假如month=calendar.get(Calendar.MONTH) + 1,就需要减一),但直接在init()中使用calendar.get(Calendar.MONTH)表示月份就无需这样的操作
完整代码
1 //3.0以上 2 //TimePicker和DatePicker选择器的监听以及Dialog对话框 3 public class MainActivity extends ActionBarActivity { 4 private TimePicker timepicker; 5 private DatePicker datepicker; 6 private Calendar cal; 7 private int year, month, day, hour, minute; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.fragment_main); 13 timepicker = (TimePicker) findViewById(R.id.timePicker1); 14 datepicker = (DatePicker) findViewById(R.id.datePicker1); 15 // 获取一个日历对象 16 cal = Calendar.getInstance(); 17 //获取年月日时分秒的信息 18 year = cal.get(Calendar.YEAR); 19 month = cal.get(Calendar.MONTH); 20 day = cal.get(Calendar.DAY_OF_MONTH); 21 hour = cal.get(Calendar.HOUR_OF_DAY); 22 minute = cal.get(Calendar.MINUTE); 23 setTitle(year + "-" + (month + 1) + "-" + day + "-" + hour + ":" + minute); 24 //------------------监听---------------------------- 25 //datepicker初始化 26 datepicker.init(year, month,day , new OnDateChangedListener() { 27 28 @Override 29 public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 30 // TODO Auto-generated method stub 31 setTitle(year + "-" + (monthOfYear+1)+"-"+dayOfMonth ); 32 } 33 }); 34 35 timepicker.setOnTimeChangedListener(new OnTimeChangedListener() { 36 37 @Override 38 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 39 // TODO Auto-generated method stub 40 setTitle(hourOfDay+":"+minute); 41 } 42 }); 43 44 45 46 47 //-----------------对话框---------------------- 48 new DatePickerDialog(this, new OnDateSetListener() { 49 50 @Override 51 public void onDateSet(DatePicker view, int year, int monthOfYear, 52 int dayOfMonth) { 53 // TODO Auto-generated method stub 54 setTitle(year + "-" + (monthOfYear+1)+"-"+dayOfMonth ); 55 } 56 }, year, month, day).show(); 57 58 new TimePickerDialog(this,new OnTimeSetListener() { 59 60 @Override 61 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 62 // TODO Auto-generated method stub 63 setTitle(hourOfDay+":"+minute); 64 } 65 }, hour, minute, true).show(); 66 67 } 68 69 }