Header ads

Header ads
» » CalendarView

CalendarView trong Android

Trong Android, Widget CalendarView được thêm vào từ API 11 (phiên bản 3.0). Do đó, CalendarView chạy trên các thiết bị có hệ đều hành phiên bản 3.0 trở lên. CalendarView  thường sử dụng để hiển thị và chọn ngày.

android.widget.calendarview là lớp con của lớp FrameLayout.

CalendarView code trong XML

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent" />

Các phương thức quan trọng của CalendarView

1. getDate(): Phương thức này được sử dụng để lấy ngày được chọn trong CalendarView, ngày được chọn theo milliseconds. Phương thức này trả về giá trị kiểu long.

Ví dụ sau chúng ta lấy giá trị ngày của CalendarView theo milliseconds

  CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView  long selectedDate = simpleCalendarView.getDate(); // get selected date in milliseconds

2. setDate(long date): Phương thức này được sử dụng để thiết lập ngày được chọn theo định dạng milliseconds

Ví dụ sau chúng ta thiết lập ngày được cho theo milliseconds cho một CalendarView.

  CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView  simpleCalendarView.setDate(1463918226920L); // set selected date 22 May 2016 in milliseconds

3. setFirstDayOfWeek(int firstDayOfWeek): Phương thức được sử dụng ngày đầu tiên trong tuần

Ví dụ sau thiết lập giá trị là 2 nghĩa là thứ 2 là ngày đầu tiên trong tuần

  CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView  simpleCalendarView.setFirstDayOfWeek(2); // Thiết lập ngày đầu tiên trong tuần là thứ 2

4. getFirstDayOfWeek(): Phương thức này lấy ngày đầu tiên trong tuần. Phương thức này trả về một số nguyên.

Code sau lấy ngày đầu tiên trong tuần

  CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView  int firstDayOfWeek= simpleCalendarView.getFirstDayOfWeek(); // Lấy ngày đầu tiên trong tuần

5. setMaxDate(long maxDate): Phương thức này thiết lập ngày tối đa trong CalendarView theo milliseconds

Ví dụ sau chúng thiết lập giá trị long tối đa cho CalendarView

  CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView  simpleCalendarView.setMaxDate(1463918226920L); // Thiết lập ngày tối đa là 22 tháng 5


Một số thuộc tính thường dùng của CalendarView

1. android:id: Là thuộc tính duy nhất của CalendarView.

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent" /> <!--  id of the CalendarView that is used to uniquely identify it -->

Dựa vào Id ta sẽ lấy được control theo đúng Id này, xem code bên dưới để biết cách lấy control theo Id:

  CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView

2. android:firstDayOfWeek: Thuộc tính này dùng để thiết lập ngày trong tiên trong tuần. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setFirstDayOfWeek(int firstDayOfWeek)

Ví dụ sau thiết lập giá trị là 2 nghĩa là thứ 2 là ngày đầu tiên trong tuần

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2" /> 

3. android:focusedMonthDateColor:  Thuộc tính này được sử dụng thiết lập các ngày trong tháng hiện tại. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setFocusedMonthDateColor(int color)

Ví dụ sau chúng ta thiết lập màu vàng cho các ngày trong tháng

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:focusedMonthDateColor="#ff0" />  <!-- Thiết lập màu vàng cho các ngày trong tháng -->

4. android:unfocusedMonthDateColor:  Thuộc tính này được sử dụng thiết lập các ngày không phải trong tháng hiện tại. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setUnfocusedMonthDateColor(int color)

Ví dụ sau chúng ta thiết lập màu đỏ cho các ngàykhông phải  trong tháng hiện tại

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:unfocusedMonthDateColor="#f00" />  <!-- thiết lập màu đỏ cho các ngày không nằm trong tháng -->

5. android:maxDate: Thuộc tính này dùng thiết lập ngày hiển thị tối đa trong CalendarView. Thuộc tính này sử dụng định dạng mm/dd/yyyy. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setMaxDate(long maxDate)

Ví dụ chúng ta thiết lập ngày hiển thị tối đa cho CalendarView là 05/22/2018

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:maxDate="05/22/2018" />  <!-- Thiết lập ngày hiển thị tối đa đến đến 22/05/2018 -->

6. android:minDate: Thuộc tính này dùng thiết lập ngày hiển thị tối thiểu trong CalendarView. Thuộc tính này sử dụng định dạng mm/dd/yyyy. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setMinDate(long minDate)

Ví dụ chúng ta thiết lập ngày hiển thị tối thiểu cho CalendarView là 05/22/2017

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:minDate="05/22/2017" />  <!-- Thiết lập ngày hiển thị tối thiểu cho CalendarView -->

7. android:selectedDateVerticalBar:  Thuộc tính này được sử dụng để thiết lập thuộc tính màu cho thanh cuộn theo chiều dọc. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setSelectedDateVerticalBar(int resourceId) hoặc phương thức setSelectedDateVerticalBar(Drawable drawable) .

Ví dụ sau chúng ta thiết lập thuộc tính màu đen cho thanh cuộc theo chiều dọc. 

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:selectedDateVerticalBar="@color/black" />

8. android:selectedWeekBackgroundColor: Thuộc tính này được sử dụng thiết lập màu nền cho tuần trong CalendarView được chọn. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setSelectedWeekBackgroundColor(int color) .

Ví dụ sau thiết lập màu nền là màu đỏ cho tuần nào được chọn

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:selectedWeekBackgroundColor="#f00" />

9. android:showWeekNumber: Thuộc tính này được sử dụng để hiển thị hoặc dấu số tuần trong CalendarView. Trong thuộc tính này nếu chúng ta thiết lập giá trị true là hiển thị, ngược lại là false là dấu. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setShowWeekNumber(boolean showWeekNumber) .

Ví dụ sau chúng ta thiết lập giá trị false để dấu số tuần hiển thị trong CalendarView

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:showWeekNumber="false" />  

10. android:weekNumberColor: Thuộc tính này sử dụng thiết lập màu cho số tuần hiển thị. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setWeekNumberColor(int color).

Ví dụ sau chúng ta thiết lập màu đỏ cho số tuần hiển thị

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:weekNumberColor="#f00" />

11. android:weekSeparatorLineColor: Thuộc tính này sử dụng để thiết lập màu đường phân cách giữa các tuần. Chúng ta cũng có thể thiết lập thuộc tính này trong java class bằng cách sử dụng phương thức setWeekSeparatorLineColor(int color) .

Ví dụ sau chúng ta thiết lập màu xanh cho đường phân cách giữa các tuần

  <CalendarView  android:id="@+id/simpleCalendarView"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:firstDayOfWeek="2"  android:weekSeparatorLineColor="#0f0" />

Ví dụ: Trong ví dụ này chúng ta sẽ làm ứng dụng gồm có một CalendarView. Trong CalendarView chúng ta thiết lập các thuộc tính: Ngày hiển thị tối thiểu và tối đa là 01/01/2018 và 01/01/2019. Chúng ta cũng thiết lập màu ngày tháng ở trạng thái focused và unfocused. Cuối cùng chúng ta thiết lập sự kiện setOnDateChangeListener cho việc thay đổi này. Khi người sử dụng tap/click để chọng ngày sẽ hiển thị trên Toast.

Tiến hành tạo project, vào thư mục  res /layout -> activity_main.xml thiết kế giao diện sau:

Bước 1: Tạo một project tên là CalendarViewFile->New->Android Application Project điền các thông tin ->Next ->Finish

Bước 2: Mở res -> layout -> xml (hoặc) activity_main.xml và thêm code, chúng ta sẽ tạo các đối tượng CanlendarView trong Relative Layout.

  <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      tools:context=".MainActivity">        <!-- CalendarView có ngày thừ là ngày đầu tiên và ngày tối thiểu, ngày tối đa -->      <CalendarView          android:id="@+id/simpleCalendarView"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:firstDayOfWeek="2"          android:maxDate="01/01/2019"          android:minDate="01/01/2018" />    </RelativeLayout>

Bước 4: Mở app -> src ->MainActivity.java và thêm code.
Trong bước này chúng ta mở MainActivity và thêm code khởi tạo CanlendarView, thiết lập màu nền cho tuần được chọn, màu đường khoảng cách giữa các tuần và cuối cùng chúng ta thiết lập sự kiện setOnDateChangeListener cho việc thay đổi này. Khi người sử dụng tap/click để chọng ngày sẽ hiển thị trên Toast

  package hiepsiit.com.calendarview;    import android.app.Activity;  import android.graphics.Color;  import android.os.Bundle;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.CalendarView;  import android.widget.Toast;    public class MainActivity extends Activity {  	CalendarView simpleCalendarView;  	@Override  	protected void onCreate(Bundle savedInstanceState) {  		super.onCreate(savedInstanceState);  		setContentView(R.layout.activity_main);  		 simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView  	        simpleCalendarView.setFocusedMonthDateColor(Color.RED); // set the red color for the dates of  focused month  	        simpleCalendarView.setUnfocusedMonthDateColor(Color.BLUE); // set the yellow color for the dates of an unfocused month  	        simpleCalendarView.setSelectedWeekBackgroundColor(Color.RED); // Thiết lập màu đỏ cho các tuần, Từ API 23 trở lên mới hỗ trợ  	        simpleCalendarView.setWeekSeparatorLineColor(Color.GREEN); // Thiết lập cho đường khoảng cách giữa các tuần là màu xanh  	        // perform setOnDateChangeListener event on CalendarView  	        simpleCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {  	            @Override  	            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {  	                // display the selected date by using a toast  	                Toast.makeText(getApplicationContext(), dayOfMonth + "/" + month + "/" + year, Toast.LENGTH_LONG).show();  	            }  	        });  	}  	  }  

Download ví dụ

Ứng dụng này được phát triển bởi adt bundleandroid 4.2 sử dụng minimum sdk 18 and target sdk 21.


Kết quả khi chạy ứng dụng:

Sau đó chọn Canlendar:



Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com

About Học viện đào tạo trực tuyến

Xinh chào bạn. Tôi là Đinh Anh Tuấn - Thạc sĩ CNTT. Email: dinhanhtuan68@gmail.com .
- Nhận đào tạo trực tuyến lập trình dành cho nhà quản lý, kế toán bằng Foxpro, Access 2010, Excel, Macro Excel, Macro Word, chứng chỉ MOS cao cấp, IC3, tiếng anh, phần mềm, phần cứng .
- Nhận thiết kế phần mềm quản lý, Web, Web ứng dụng, quản lý, bán hàng,... Nhận Thiết kế bài giảng điện tử, số hóa tài liệu...
HỌC VIỆN ĐÀO TẠO TRỰC TUYẾN:TẬN TÂM-CHẤT LƯỢNG.
«
Next
Bài đăng Mới hơn
»
Previous
Bài đăng Cũ hơn