自定义带DatePickerDialog

注意:这里的Dialog_Common是自定义的,请看我的另外一篇自定义Dialog_Common,下面是点击按钮弹出对话框,设定的时间以Toast形式显示,布局我就不给了


  but8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				MyDialog_Common.Builder dialog = new MyDialog_Common.Builder(DialogTestActivity.this);
				DatePicker picker = new DatePicker(DialogTestActivity.this);
				final Calendar calendar = Calendar.getInstance(Locale.CHINA);
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH);
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				picker.init(year, month, day, new OnDateChangedListener() {

					@Override
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
					
						calendar.set(Calendar.YEAR, year);
						calendar.set(Calendar.MONTH, monthOfYear);
						calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
					}
					
				});
				dialog.setTitle("设置日期")
				.setContentView(picker)
				.setFirstButton("设定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						int year = calendar.get(Calendar.YEAR);
						int month = calendar.get(Calendar.MONTH);
						int day = calendar.get(Calendar.DAY_OF_MONTH);
						Toast.makeText(DialogTestActivity.this, "现在时间: " + year + month + day , Toast.LENGTH_SHORT).show();
						dialog.dismiss();
						
					}
					
				})
				.setSecondButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						dialog.dismiss();
					}
				}).create().show();
			}
        	
        });


你可能感兴趣的:(Android开发相关)