1.介绍DatePicker和TimePicker两种实现动态输入日期和时间的功能
2.介绍DataPickerDialog和TimePickerDialog两种实现动态输入日期和时间的对话框
3,介绍两组针对监测日期时间改变的监听器包含:
OnDataChangedListener和OnTimeChangedListener()
OnDataSetListener和OnTimeSetListener()
>当用户更改了DataPicker里的年,月,日时,将触发OnDataChangedListener监听器的OnDataChange()事件
>当用户更改了TimePicker里的时,分时,将触发OnTimeChangedListener监听器的OnTimeChanged()事件
>当用户更改了DataPickerDialog里的年,月,日时,将触发OnDataSetListener监听器的onDataSet()事件
>当用户更改了TimePickerDialog里的时,分时,将触发OnTimeSetListener监听器的onTimeSet()事件
1.xml布局:两个空间DatePicker 和TimePicker
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
2.java代码部分
public class MainActivity extends Activity {
private DatePicker datePicker;
private TimePicker timePicker;
private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取一个日历对象
cal = Calendar.getInstance();
//获取年月日时分秒的信息
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
setTitle(year + "-" + month + "-" + day + " " + hour + ":" + minute);
datePicker = (DatePicker) findViewById(R.id.datePicker1);
timePicker = (TimePicker) findViewById(R.id.timePicker1);
//datePicker初始化
datePicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
}
});
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
setTitle(hourOfDay + ":" + minute);
}
});
new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
}
}, year, cal.get(Calendar.MONTH), day).show();
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
setTitle(hourOfDay + ":" + minute);
}
}, hour, minute, true).show();
}
}
代码下载:http://download.csdn.net/detail/weimo1234/8423223