activity代码
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.form_widget); showDatePicker(); } private void showDatePicker() { // TODO Auto-generated method stub DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker1); //得到系统时间 Calendar calendar = Calendar.getInstance(); datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() { public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub Toast.makeText(FormWidgetActivity.this, "Year:"+year+" Month"+monthOfYear+" Day"+dayOfMonth, 3000).show(); } }); }
xml代码:
<DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" />