Android實現日期時間選擇對話框

日期/時間選擇對話框(DatePickerDialog和TimePickerDialog)的使用,供大傢參考,具體內容如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <EditText 
        android:id="@+id/edit"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="日期選擇器" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="時間選擇器" />
 
</LinearLayout>
public class MainActivity extends Activity {
 // 實例化控件
 private Button dateButton;
 private Button timeButton;
 private EditText editText;
 private DatePickerDialog dateDialog;
 private TimePickerDialog timeDialog;
 private int year, monthOfYear, dayOfMonth, hourOfDay, minute;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // 通過findViewById找到控件
  dateButton = (Button) findViewById(R.id.button1);
  timeButton = (Button) findViewById(R.id.button2);
  editText = (EditText) findViewById(R.id.edit);
  // 通過Calendar對象來獲取年、月、日、時、分的信息
  Calendar calendar = Calendar.getInstance();
  year = calendar.get(calendar.YEAR);
  monthOfYear = calendar.get(calendar.MONTH);
  dayOfMonth = calendar.get(calendar.DAY_OF_MONTH);
  hourOfDay = calendar.get(calendar.HOUR_OF_DAY);
  minute = calendar.get(calendar.MINUTE);
  /*
   * 實例化DatePickerDialog
   */
  dateDialog = new DatePickerDialog(this, new OnDateSetListener() {
 
   @Override
   public void onDateSet(DatePicker arg0, int year, int monthOfYear,
     int dayOfMonth) {
    // 把獲取的日期顯示在文本框內,月份從0開始計數,所以要加1
    String text = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
    editText.setText(text);
   }
  }, year, monthOfYear, dayOfMonth); // 後面的三個參數對應於上面的年、月、日
  /**
   * 對日期選擇器按鈕設置監聽事件
   */
  dateButton.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View arg0) {
    // 點擊日期選擇器按鈕時顯示出日期對話框
    dateDialog.show();
   }
  });
  /*
   * 實例化TimePickerDialog
   */
  timeDialog = new TimePickerDialog(this, new OnTimeSetListener() {
 
   @Override
   public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, hourOfDay + ":" + minute,
      Toast.LENGTH_LONG).show();
   }
  }, hourOfDay, minute, true); // 最後一個參數設置是否為24小時制
  /**
   * 對時間選擇器按鈕設置監聽事件
   */
  timeButton.setOnClickListener(new View.OnClickListener() {
 
   @Override
   public void onClick(View arg0) {
    // 點擊時間選擇器按鈕時顯示出時間對話框
    timeDialog.show();
   }
  });
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: