模拟小时钟控件:AnalogClock
数字小时钟控件:DigitalClock
以上两个都直接在布局文件里面放置即可
动态输入日期和时间:
DatePicker控件可以动态设置日期,用于用户更改年月日,DatePicker控件设置监听必须通过init()方法,传入默认初始值。
((DatePicker) findViewById(R.id.dp)).init(mYear, mMonth, mDay, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //日期被修改时调用 } });
TimePicker控件可以让用户动态更改时间:
((TimePicker) findViewById(R.id.tp)) .setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { //时间修改时调用 } });
DatePickerDialog和TimePickerDialog也可以实现日期和时间的更改,只是这种是以对话框的方式来让用户进行更改,具体用哪种,需更改需求来定,示例如下:
findViewById(R.id.bt_date).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Calendar calendar = Calendar.getInstance(); new DatePickerDialog(DatePickerDialogActivity.this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub Log.e("-----------------", year + "/" + monthOfYear + "/" + dayOfMonth); } }, calendar.get(Calendar.YEAR), calendar .get(Calendar.MONTH), calendar .get(Calendar.DAY_OF_MONTH)).show(); } }); findViewById(R.id.bt_time).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Calendar calendar = Calendar.getInstance(); new TimePickerDialog(DatePickerDialogActivity.this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub Log.e("-----------------", hourOfDay + ":" + minute); } }, calendar.get(Calendar.HOUR_OF_DAY), calendar .get(Calendar.MINUTE), true).show(); } });