1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package com.urovo.stocity.view.custom; import java.util.Calendar; import java.util.Date; import utils.DateUtil; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker;
public class DateTimeEditText extends EditText implements OnDateSetListener, OnTimeSetListener { public MyDatePickDialog Dlg; public Dialog timeDialog; private Context context; private String timeStr; public DateTimeEditText(Context context) { super(context); this.context = context; setListener(); } public DateTimeEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; setListener(); } private void setListener() { this.setFocusable(false); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { initCalendar(); Dlg.show(); } }); } public void dateSelect() { initCalendar(); Dlg.show(); } private void initCalendar() { String dateStr = this.getText().toString().trim(); Date date = DateUtil.parseCompleteDate(dateStr); if (date == null) { date = new Date(); } Calendar d = Calendar.getInstance(); d.setTimeInMillis(date.getTime()); int year = d.get(Calendar.YEAR); int month = d.get(Calendar.MONTH); int day = d.get(Calendar.DAY_OF_MONTH); Dlg = new MyDatePickDialog(context, this, year, month, day); Dlg.setCanceledOnTouchOutside(false); timeDialog = new MyTimePickerDialog(context, this, d.get(Calendar.HOUR_OF_DAY), d.get(Calendar.MINUTE), true); timeDialog.setTitle("请选择时间"); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { timeStr = year + "-" + (monthOfYear < 9 ? "0" + (monthOfYear + 1) : monthOfYear + 1) + "-" + (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth) + " "; timeDialog.show(); Log.e("aa", "onDateSet"); } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { timeStr += (hourOfDay < 10 ? "0" + hourOfDay : hourOfDay) + ":" + (minute < 10 ? "0" + minute : minute) + ":00"; DateTimeEditText.this.setText(timeStr); Log.e("aa", "onTimeSet"); } public static class MyDatePickDialog extends DatePickerDialog { public MyDatePickDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, callBack, year, monthOfYear, dayOfMonth); } public MyDatePickDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, theme, callBack, year, monthOfYear, dayOfMonth); } @Override protected void onStop() { } } public static class MyTimePickerDialog extends TimePickerDialog { public MyTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) { super(context, callBack, hourOfDay, minute, is24HourView); } public MyTimePickerDialog(Context context, int theme, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) { super(context, theme, callBack, hourOfDay, minute, is24HourView); } @Override protected void onStop() { } } }
|