东学西读
效果图
package com.example.liuqidemo.view;import java.text.DateFormat;
import java.text.ParseException;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.Date;
import com.example.vehiclemandcsyh.R;
import Android.content.context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
public class DateTimePickerDialog {
private DatePicker dp;
private TimePicker tp;
private Context context;
private MyDialog dateDialog;
private TextView picktitle;
private String dateString="";
private EditText editText;
private String formStr="";// 输出格式
private int type;// 操作类型
private int count=1;
public DateTimePickerDialog(Context context){
this.context=context;
}
/**
* 设置日期和时间(无初始化 日期时间参数),typ=1:时间选择;type=2:日期选择;type=3:日期时间选择
* @param editText
*/
public void getDateTimePicker(EditText editText,int type){
this.type=type;
int year, month, day;
this.editText=editText;
Calendar calendar=Calendar.getinstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);
dateDialog= new MyDialog(context, R.layout.zsg_datetime_pick);
picktitle=(TextView)dateDialog.findViewById(R.id.tv_datetime_pick_title);
dp=(DatePicker)dateDialog.findViewById(R.id.datepicker);
tp=(TimePicker)dateDialog.findViewById(R.id.timepicker);
switch (this.type) {
case 1:
tp.setVisibility(View.VISIBLE);
dp.setVisibility(View.GONE);
picktitle.setText("请选择时间");
formStr="HH:mm";
break;
case 2:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd";
picktitle.setText("请选择日期");
break;
default:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd HH:mm";
picktitle.setText("请选择日期");
break;
}
dp.setCalendarViewShown(false);
dp.init(year, month, day, new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
}
});
int a=tp.getCurrentHour()+0;
tp.setIs24HourView(true);
tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
}
});
tp.setCurrentHour(a);
Button btn_sure=(Button)dateDialog.findViewById(R.id.btn_datepicker_sure);
btn_sure.setOnClickListener(sureListener);
Button btn_cancel=(Button)dateDialog.findViewById(R.id.btn_datepicker_cancel);
btn_cancel.setOnClickListener(cancelListener);
}
private OnClickListener sureListener = new OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
if(type==1 || type==2){
calendar.set(dp.getYear(), dp.getmonth(),
dp.getDayOfMonth(), tp.getCurrentHour(),
tp.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat(formStr);
dateString= sdf.format(calendar.getTime());
editText.setText(dateString);
Log.i("onTouch", dateString+"--");
dateDialog.cancel();
}else {
if(count<2){
count++;
tp.setVisibility(View.VISIBLE);
dp.setVisibility(View.GONE);
picktitle.setText("请选择时间");
}else{
count=1;
calendar.set(dp.getYear(), dp.getMonth(),
dp.getDayOfMonth(), tp.getCurrentHour(),
tp.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat(formStr);
dateString= sdf.format(calendar.getTime());
editText.setText(dateString);
Log.i("onTouch", dateString+"--");
dateDialog.cancel();
}
}
}
};
private OnClickListener cancelListener = new OnClickListener() {
@Override
public void onClick(View v) {
dateDialog.cancel();
}
};
/**
* 设置日期和时间(无初始化 日期时间参数),typ=1:时间选择;type=2:日期选择;type=3:日期时间(无秒)选择,type=4 日期时间(含秒)选择
* minDate,maxDate 设置 日期选择范围,值为0则不设置(使用默认)
* @param editText
*/
public void getDateTimePicker(EditText editText,int type,long minDate,long maxDate, final int minHour,final int maxHour){
this.type=type;
int year, month, day;
this.editText=editText;
Calendar calendar=Calendar.getInstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);
dateDialog= new MyDialog(context, R.layout.zsg_datetime_pick);
picktitle=(TextView)dateDialog.findViewById(R.id.tv_datetime_pick_title);
dp=(DatePicker)dateDialog.findViewById(R.id.datepicker);
tp=(TimePicker)dateDialog.findViewById(R.id.timepicker);
if (minDate!=0) {
dp.setMinDate(minDate);
}
if (maxDate!=0) {
dp.setMaxDate(maxDate);
}
switch (this.type) {
case 1:
tp.setVisibility(View.VISIBLE);
dp.setVisibility(View.GONE);
picktitle.setText("请选择时间");
formStr="HH:mm";
break;
case 2:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd";
picktitle.setText("请选择日期");
break;
case 3:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd HH:mm";
picktitle.setText("请选择日期");
break;
default:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd HH:mm:ss";
picktitle.setText("请选择日期");
}
dp.setCalendarViewShown(false);
dp.init(year, month, day, new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
}
});
int a=tp.getCurrentHour()+0;
tp.setIs24HourView(true);
tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
if (hourOfDay>maxHour) {
view.setCurrentHour(maxHour);
}
if (hourOfDay<minHour) {
view.setCurrentHour(minHour);
}
}
});
tp.setCurrentHour(a);
Button btn_sure=(Button)dateDialog.findViewById(R.id.btn_datepicker_sure);
btn_sure.setOnClickListener(sureListener);
Button btn_cancel=(Button)dateDialog.findViewById(R.id.btn_datepicker_cancel);
btn_cancel.setOnClickListener(cancelListener);
}
/**
* 设置日期和时间(有初始化 日期时间参数(从 editText 中获取)),typ=1:时间选择;type=2:日期选择;type=3:日期时间(无秒)选择,type=4 日期时间(含秒)选择
* minDate,maxDate 设置 日期选择范围,值为0则不设置(使用默认); strForm: 时间格式
* @param editText
*/
public void getDateTimePicker(EditText editText,int type,long minDate,long maxDate, final int minHour,final int maxHour , boolean isDefault){
this.type=type;
int year, month, day;
this.editText=editText;
Calendar calendar=Calendar.getInstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);
dateDialog= new MyDialog(context, R.layout.zsg_datetime_pick);
picktitle=(TextView)dateDialog.findViewById(R.id.tv_datetime_pick_title);
dp=(DatePicker)dateDialog.findViewById(R.id.datepicker);
tp=(TimePicker)dateDialog.findViewById(R.id.timepicker);
if (minDate!=0) {
dp.setMinDate(minDate);
}
if (maxDate!=0) {
dp.setMaxDate(maxDate);
}
switch (this.type) {
case 1:
tp.setVisibility(View.VISIBLE);
dp.setVisibility(View.GONE);
picktitle.setText("请选择时间");
formStr="HH:mm";
break;
case 2:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd";
picktitle.setText("请选择日期");
break;
case 3:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd HH:mm";
picktitle.setText("请选择日期");
break;
default:
tp.setVisibility(View.GONE);
dp.setVisibility(View.VISIBLE);
formStr="yyyy-MM-dd HH:mm:ss";
picktitle.setText("请选择日期");
}
if (isDefault) {
DateFormat df = new SimpleDateFormat(formStr);
try {
Date d = df.parse(editText.getText().toString());
calendar.setTime(d);
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printstacktrace();
}
}
dp.setCalendarViewShown(false);
dp.init(year, month, day, new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
}
});
int a=tp.getCurrentHour()+0;
tp.setIs24HourView(true);
tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
if (hourOfDay>maxHour) {
view.setCurrentHour(maxHour);
}
if (hourOfDay<minHour) {
view.setCurrentHour(minHour);
}
}
});
tp.setCurrentHour(a);
if (isDefault) {
tp.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
tp.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
Log.i("onTouch", calendar.get(Calendar.HOUR_OF_DAY)+"--"+calendar.get(Calendar.MINUTE)+"~"+ calendar.get(Calendar.SECOND));
Button btn_sure=(Button)dateDialog.findViewById(R.id.btn_datepicker_sure);
btn_sure.setOnClickListener(sureListener);
Button btn_cancel=(Button)dateDialog.findViewById(R.id.btn_datepicker_cancel);
btn_cancel.setOnClickListener(cancelListener);
}
}
相关阅读
先简要介绍一下:算法复杂度分为时间复杂度和空间复杂度。其作用:时间复杂度是指执行这个算法所需要的计算工作量;而空间复杂度是指执
920备货节终于来了,还有不知道的小伙伴吗?今年920大促的主题是“全球双十一备货节”,大家可别小瞧了920备货节哦,它的销售情况可以影
为大家推荐如下
双十一刚刚进入尾声,人们都进入了等待快递的过程,物流公司日以继夜的在忙着,转眼再过一个月就是双十二了,很多人担心自己的快递不能按
//服务器返回的值往往是秒,但是计算的时候要求毫秒,需要*1000L才能得到正确的日期结果。SimpleDateFormat sdf = new SimpleDateFor