Android 入门基础知识
---DatePickerDialog汇总
DatePicker,TimePicker或DatePIckerDialog,TimePickerDialog使用类似,这里只描述DatePickerDialog的基本使用。
基本格式为:
NewDatePickerDialog(this,listener,defauleYear,defauleMonth,defaulDay).show();
listener为监听器,当user对dialog做出选择是做出判段:
listener newDatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerview, int year, int monthOfYear,
int dayOfMonth) {
//可以获得user设置的的 year monthofYear,这里还可以对数据做出其他操作
}
}
publicvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar=Calendar.getInstance(); btn_dialog=(Button)findViewById(R.id.btn_dialog); btn_dialog.setOnClickListener(newView.OnClickListener() { @Override publicvoidonClick(View v) { showDialog(DIALOG_DATEPICKER); } }); } @Override protectedDialog onCreateDialog(intid, Bundle args) { //TODO Auto-generated method stub switch(id) { caseDIALOG_DATEPICKER: returnnewDatePickerDialog(DatePickerDialogDemo.this, newDatePickerDialog.OnDateSetListener() { @Override publicvoidonDateSet(DatePickerview, intyear, intmonthOfYear, intdayOfMonth) { /* *这是设置好日期后的回调函数, *可以根据设置的内容更新用户界面 *或者系统时间。 */ } },calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)); default: returnnull; } }
new DatePickerDialog(FirstActivity.this, new DatePickerDialog.OnDateSetListener() { @Override publicvoid onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub year_user = year; } },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();