Code example for DatePickerDialog

Methods: setButtonsetOnCancelListenershow

0
	public void onClick(View v) {
		final Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);
		DatePickerDialog dlg = new DatePickerDialog(getContext(), new OnDateSetListener() {
			 
			@Override 
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				System.out.println("year=" + year + ", month=" + monthOfYear + ", day=" + dayOfMonth);
				Calendar ca = Calendar.getInstance();
				ca.set(Calendar.YEAR, year);
				ca.set(Calendar.MONTH, monthOfYear);
				ca.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				if (ca.getTimeInMillis() < c.getTimeInMillis() && !canSetBefore) {
					Toast.makeText(getContext(), "日期不能往后设置", Toast.LENGTH_LONG).show();
				} else { 
					setDate(year, monthOfYear + 1, dayOfMonth);
				} 
			} 
		}, y, m, d);
		dlg.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", onClickListener);
		dlg.setOnCancelListener(onCancelListener);
		dlg.show();
	} 
	 
	private android.content.DialogInterface.OnClickListener onClickListener = new android.content.DialogInterface.OnClickListener() {
		 
		@Override 
Stop searching for code, let great code find you!  Add Codota to your java IDE