标签:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.app.DatePickerDialog; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.bjjw.cjgc.R; public class SelectDateView extends LinearLayout { public interface OnButtonClickListener { /** * @param startDate 开始日期 * @param endDate 结束日期 */ public void onOKClick(String startDate, String endDate); public void onCancelClick(); } public interface OnStartDateClickListener { public void onStartDateClick(); } public interface OnEndDateClickListener { public void onEndDateClick(); } private OnButtonClickListener buttonClickListener = null; private OnStartDateClickListener onStartDateClickListener = null; private OnEndDateClickListener onEndDateClickListener = null; private Context context; private TextView selectDate; private TextView selectDateEnd; private Button commit; private Button cancel; // private int selectYear; // private int selectMonth; // private int selectDay; private DatePickerDialog datePicker; private DatePickerDialog datePickerEnd; private Date dateE; private Date dateS; private Date now; private Calendar calendar; private SimpleDateFormat sdf; private String startDate; private String endDate; private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // selectYear = year; // selectMonth = monthOfYear; // selectDay = dayOfMonth; dateS.setYear(year - 1900); dateS.setMonth(monthOfYear); dateS.setDate(dayOfMonth); // startDate = getDateString(year, monthOfYear, dayOfMonth); // selectDate.setText(startDate); calendar.set(year, monthOfYear, dayOfMonth); Date date = calendar.getTime(); selectDate.setText(sdf.format(date)); startDate = sdf.format(date); } }; private DatePickerDialog.OnDateSetListener listenerEnd = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // selectYear = year; // selectMonth = monthOfYear; // selectDay = dayOfMonth; dateE.setYear(year - 1900); dateE.setMonth(monthOfYear); dateE.setDate(dayOfMonth); calendar.set(year, monthOfYear, dayOfMonth); Date date = calendar.getTime(); selectDateEnd.setText(sdf.format(date)); calendar.add(Calendar.DAY_OF_YEAR, 1); date = calendar.getTime(); endDate = sdf.format(date); } }; public SelectDateView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public SelectDateView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public SelectDateView(Context context) { super(context); init(context); } private void init(Context context) { this.context = context; View contentView = LayoutInflater.from(context).inflate(R.layout.select_date_view, this); selectDate = (TextView) contentView.findViewById(R.id.date); selectDateEnd = (TextView) contentView.findViewById(R.id.date_end); commit = (Button) contentView.findViewById(R.id.commit); cancel = (Button) contentView.findViewById(R.id.cancel); dateE = new Date(); dateS = new Date(); calendar = Calendar.getInstance(); now = calendar.getTime(); sdf = new SimpleDateFormat("yyyy-MM-dd"); int selectYear = calendar.get(Calendar.YEAR); int selectMonth = calendar.get(Calendar.MONTH); int selectDay = calendar.get(Calendar.DAY_OF_MONTH); datePicker = new DatePickerDialog(context, listener, selectYear, selectMonth, selectDay); datePickerEnd = new DatePickerDialog(context, listenerEnd, selectYear, selectMonth, selectDay); String btnText = sdf.format(now); endDate = btnText; startDate = btnText; selectDate.setText(btnText); selectDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { datePicker.show(); // 可以在下面的代码设置自定义的日期选择组件 // if (null != onStartDateClickListener) { // onStartDateClickListener.onStartDateClick(); // } } }); selectDateEnd.setText(btnText); selectDateEnd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { datePickerEnd.show(); // 可以在下面的代码设置自定义的日期选择组件 // if (null != onEndDateClickListener) { // onEndDateClickListener.onEndDateClick(); // } } }); commit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { errorSelect(); } }); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (null != buttonClickListener) { buttonClickListener.onCancelClick(); } } }); } private String getDateString(int year, int month, int day) { return year + "-" + (month + 1) + "-" + day; } /** * 2014-09-12 * 2014-09-28 * 判断日期选择是否正确 */ private void errorSelect() { String text = ""; String startError = "开始时间请选择不超过结束时间的日期"; String endError = "结束时间请选择不超过今天的日期"; try { dateE = new SimpleDateFormat("yyyy-MM-dd").parse(selectDateEnd.getText().toString()); dateS = new SimpleDateFormat("yyyy-MM-dd").parse(selectDate.getText().toString()); } catch (ParseException e) { Toast.makeText(context, "日期格式不正确", Toast.LENGTH_LONG).show(); return; } if (dateE.compareTo(now) > 0) { // text = endError; text=""; } if (dateS.compareTo(dateE) > 0) { if (!"".equals(text)) { text += ", " + startError; } else { text = startError; } } if (!"".equals(text)) { Toast.makeText(context, text, Toast.LENGTH_LONG).show(); } else { if (null != buttonClickListener) { buttonClickListener.onOKClick(selectDate.getText().toString(), selectDateEnd.getText().toString()); } } } public TextView getSelectDate() { return selectDate; } public TextView getSelectDateEnd() { return selectDateEnd; } public void setOnButtonClickListener(OnButtonClickListener buttonClickListener) { this.buttonClickListener = buttonClickListener; } public void setOnStartDateClickListener( OnStartDateClickListener onStartDateClickListener) { this.onStartDateClickListener = onStartDateClickListener; } public void setOnEndDateClickListener( OnEndDateClickListener onEndDateClickListener) { this.onEndDateClickListener = onEndDateClickListener; } }
标签:
原文地址:http://www.cnblogs.com/chenlong-50954265/p/4998279.html