xml
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出日期对话框" android:onClick="rq_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出时间对话框" android:onClick="sj_onclick"/>
java
public void rq_onclick(View v) { //获取当前日期 //单例模式,设计模式的一种 静态方法 Calendar c = Calendar.getInstance(); DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(UIActivity2.this, year+"-"+ (monthOfYear+1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show(); } },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)); //点击其他部分不消失 dpd.setCancelable(false); dpd.show(); } public void sj_onclick(View v) { //获取当前日期 //单例模式,设计模式的一种 静态方法 Calendar c = Calendar.getInstance(); TimePickerDialog tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(UIActivity2.this, hourOfDay+":"+minute , Toast.LENGTH_SHORT).show(); } },c.get(Calendar.HOUR),c.get(Calendar.MINUTE),true); tp.setCancelable(false); tp.show(); }