Date Picker trong Android



Date Picker trong Android cho phép bạn lựa chọn date bao gồm ngày, tháng, và năm trong Custom UI của bạn. Với tính năng này, Android cung cấp các thành phần DatePicker và DatePickerDialog.

Trong chương này, chúng tôi minh họa cách sử dụng của DatePicker thông qua DatePickerDialog. DatePickerDialog là một hộp thoại đơn giản chứa DatePicker.

Để hiển thị DatePickerDialog, bạn phải truyền DatePickerDialog ID tới phương thức Cú pháp như sau: showDialog(id_of_dialog) Cú pháp như sau: −

showDialog(999);

Khi gọi phương thức showDialog này, một phương thức khác cũng tự động được gọi là onCreateDialog Vì thế, chúng ta phải ghi đè cả phương thức đó. Cú pháp như sau: −

@Override
protected Dialog onCreateDialog(int id) {
   // TODO Auto-generated method stub
   if (id == 999) {
      return new DatePickerDialog(this, myDateListener, year, month, day);
   }
   return null;
}

Trong bước cuối cùng, bạn phải đăng ký DatePickerDialog Listener và ghi đè phương thức onDateSet của nó. Phương thức onDateSet này chứa ngày, tháng, và năm đã được cập nhật. Cú pháp như sau: −

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
      // arg1 = year
      // arg2 = month
      // arg3 = day		
   }
};
Date Picker trong Android

Ngoài các thuộc tính date, đối tượng DatePicker cũng được truyền vào trong hàm này. Bạn có thể sử dụng các phương thức sau của DatePicker để thực hiện các hoạt động khác.

Stt Phương thức & Miêu tả
1 getDayOfMonth()

Phương thức này lấy ngày đã chọn của tháng

2 getMonth()

Phương thức này lấy tháng đã chọn

3 getYear()

Phương thức này lấy năm đã chọn

4 setMaxDate(long maxDate)

Phương thức này thiết lập date tối đa được hỗ trợ bởi DatePicker (giá trị mili giây) từ 1/1/1970 00:00:00 trong getDefault()

5 setMinDate(long minDate)

Phương thức này thiết lập date tối thiểu được hỗ trợ bởi DatePicker (giá trị mili giây) từ 1/1/1970 00:00:00 trong getDefault()

6 setSpinnersShown(boolean shown)

Phương thức này thiết lập xem có hay không spinner được hiển thị

7 updateDate(int year, int month, int dayOfMonth)

Phương thức này cập nhật date hiện tại

8 getCalendarView()

Phương thức này trả về calendar view

9 getFirstDayOfWeek() Phương thức này trả về ngày đầu tiên của tuần

Ví dụ

Bây giờ bạn có thể quan sát rằng date đã được thiết lập tại dưới cùng. Bây giờ chúng ta sẽ thay đổi date thông qua DatePickerDialog bằng việc nhấn nút Set Date. Khi nhấn nút, màn hình sau sẽ hiện ra:

Bây giờ thiết lập date cần thiết, và sau khi thiết lập xong, nhấn nút Done. Hộp thoại này sẽ biến mất và date mới được thiết lập sẽ bắt đầu hiển thị tại màn hình. Như sau:

Sau đây là nội dung của Main Activity file đã được sửa đổi: src/com.example.datepicker/MainActivity.java.

package com.example.datepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {   private DatePicker datePicker;
   private Calendar calendar;
   private TextView dateView;
   private int year, month, day;   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      dateView = (TextView) findViewById(R.id.textView3);
      calendar = Calendar.getInstance();
      year = calendar.get(Calendar.YEAR);
      
      month = calendar.get(Calendar.MONTH);
      day = calendar.get(Calendar.DAY_OF_MONTH);
      showDate(year, month+1, day);
   }   @SuppressWarnings("deprecation")
   public void setDate(View view) {
      showDialog(999);
      Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
      .show();
   }   @Override
   protected Dialog onCreateDialog(int id) {
      // TODO Auto-generated method stub
      if (id == 999) {
         return new DatePickerDialog(this, myDateListener, year, month, day);
      }
      return null;
   }   private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
      @Override
      public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
         // TODO Auto-generated method stub
         // arg1 = year
         // arg2 = month
         // arg3 = day
         showDate(arg1, arg2+1, arg3);
      }
   };   private void showDate(int year, int month, int day) {
      dateView.setText(new StringBuilder().append(day).append("/")
      .append(month).append("/").append(year));
   }   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

Bạn sửa đổi nội dung của res/layout/activity_main.xml.

   

Và đây là nội dung của res/values/string.xml.



   DatePicker
   Settings
   Hello world!
   Press the button to set the date
   Set Date
   The Date is: 
   

Chạy ứng dụng DatePicker vừa sửa đổi ở trên.

Date Picker trong Android
user_interface_trong_android.jsp