package org.ben.test; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; public class AndroidTest_DatePicker_TimePickerActivity extends Activity { /** Called when the activity is first created. */ TextView textView; DatePicker datePicker; TimePicker timePicker; Button dpBtn; Button tpBtn; Calendar calendar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar = Calendar.getInstance(); textView = (TextView)findViewById(R.id.textview); dpBtn = (Button)findViewById(R.id.dateBtn); tpBtn = (Button)findViewById(R.id.timeBtn); datePicker = (DatePicker)findViewById(R.id.datePicker); timePicker = (TimePicker)findViewById(R.id.timePicker); datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub //修改日期 calendar.set(year, monthOfYear, dayOfMonth); } }); //设置按24小时显示 timePicker.setIs24HourView(true); timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){ public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { calendar.set( hourOfDay, minute); } }); dpBtn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { new DatePickerDialog(AndroidTest_DatePicker_TimePickerActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub //设置日期 datePicker.updateDate(year, monthOfYear, dayOfMonth); } },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show(); } }); tpBtn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { new TimePickerDialog(AndroidTest_DatePicker_TimePickerActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub //设置时间 timePicker.setCurrentHour(hourOfDay); timePicker.setCurrentMinute(minute); } },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show(); } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/dateBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置日期"/> <Button android:id="@+id/timeBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置时间"/> </LinearLayout>