work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android DatePicker, TimePicker控件的使用

Posted on 2013-03-08 09:41  work hard work smart  阅读(600)  评论(0编辑  收藏  举报

Android DatePicker, TimePicker控件的使用

public class TimerPickerActivity extends Activity{

	private DatePicker datePicker;
	private TimePicker timePicker;
	private static Calendar calendar = Calendar.getInstance();
	@Override
	  protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.timepicker);
			
			timePicker = (TimePicker)findViewById(R.id.timePicker1);
			datePicker = (DatePicker)findViewById(R.id.datePicker1);
			
			timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
				
				@Override
				public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
					// TODO Auto-generated method stub
					StringBuffer buffer = new StringBuffer();
					buffer.append(hourOfDay < 10 ? "0" + hourOfDay:hourOfDay );
					buffer.append(":").append(minute);
					Toast.makeText(TimerPickerActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show();
				}
			});
			
			datePicker = (DatePicker)findViewById(R.id.datePicker1);
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH);
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			
			datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
				
				@Override
				public void onDateChanged(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					
				}
			});
	}
}