一、日期設置控件:DatePickerDialogjava
二、時間設置控件:TimePickerDialogandroid
實例代碼app
一、頁面添加兩個Button,單擊分別顯示日期設置控件和時間設置控件,仍是有TextView控件,用於顯示設置後的系統時間ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:id="@+id/dateAndTime" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="@string/hello" /> <Button Android:id="@+id/setDate" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="Set the Date"></Button> <Button Android:id="@+id/setTime" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="Set the Time"></Button> </LinearLayout>
package yyl.Android; import java.text.DateFormat; import java.util.Calendar; import java.util.Locale; 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 ChronoDemo extends Activity { //獲取日期格式器對象 DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance(); //定義一個TextView控件對象 TextView dateAndTimeLabel = null; //獲取一個日曆對象 Calendar dateAndTime = Calendar.getInstance(Locale.CHINA); //當點擊DatePickerDialog控件的設置按鈕時,調用該方法 DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //修改日曆控件的年,月,日 //這裏的year,monthOfYear,dayOfMonth的值與DatePickerDialog控件設置的最新值一致 dateAndTime.set(Calendar.YEAR, year); dateAndTime.set(Calendar.MONTH, monthOfYear); dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth); //將頁面TextView的顯示更新爲最新時間 updateLabel(); } }; TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() { //同DatePickerDialog控件 @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay); dateAndTime.set(Calendar.MINUTE, minute); updateLabel(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲得頁面設定日期的按鈕控件對象 Button dateBtn = (Button)findViewById(R.id.setDate); //設置按鈕的點擊事件監聽器 dateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //生成一個DatePickerDialog對象,並顯示。顯示的DatePickerDialog控件能夠選擇年月日,並設置 new DatePickerDialog(ChronoDemo.this, d, dateAndTime.get(Calendar.YEAR), dateAndTime.get(Calendar.MONTH), dateAndTime.get(Calendar.DAY_OF_MONTH)).show(); } }); Button timeBtn = (Button)findViewById(R.id.setTime); timeBtn.setOnClickListener(new View.OnClickListener() { //同上原理 @Override public void onClick(View v) { new TimePickerDialog(ChronoDemo.this, t, dateAndTime.get(Calendar.HOUR_OF_DAY), dateAndTime.get(Calendar.MINUTE), true).show(); } }); dateAndTimeLabel=(TextView)findViewById(R.id.dateAndTime); updateLabel(); } //更新頁面TextView的方法 private void updateLabel() { dateAndTimeLabel.setText(fmtDateAndTime .format(dateAndTime.getTime())); } }