日期对话框和时间对话框道理一样,只是用到的方法不一样,但是格式是完全一样的
日期对话框:
public DatePickerDialog(Context context,OnDateSetListener callBack,//点击确定的回调监听
int year,//显示年份
int monthOfYear,//显示月份
int dayOfMonth)//显示日
时间对话框
public TimePickerDialog(Context context,OnTimeSetListener callBack,//点击确定的回调监听
int hourOfDay,//设置小时
int minute,//设置分钟
boolean is24HourView)//是否是24小时制
xml代码
<Button
android:id="@+id/btn_Datedialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示DataPickerDialog"
android:layout_marginTop="30dp"
android:onClick="showDP"/>
<Button
android:id="@+id/btn_TimePickerDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示TimePickerDialog"
android:onClick="showTP"/>
//设置时间对话框
public void showTP(View view) {
Calendar calendar = Calendar.getInstance();
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);//得到小时
int minute = calendar.get(Calendar.MINUTE);//得到小时
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, hourOfDay, minute, true).show();
}
//设置日期对话框
public void showDP(View v) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int monthOfYear = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
}
}, year, monthOfYear, dayOfMonth).show();
}