Android development-calendar control implementation

Source: Internet
Author: User

This topic describes several calendar controls on the Android platform. Calendar controls have many solutions in Web development and are easy to implement. However, there are few and not easy to implement solutions on the Android platform. The following describes several examples and their implementation.

 

As we all know, the calendar view control is added in Android 3.0 to display grid-like calendar content. For Android 3.0 or lower versions, the calendar control can only be used by third parties, currently, CalendarView is the most used. First, we will briefly introduce the use of the CalendarView calendar control.

Android. widget. CalendarView is inherited from android. widget. FrameLayout.

 

The CalendarView class provides the basic calendar setting method, as shown in the following code:
Long getDate (); // obtain the number of milliseconds from January 1, 1970, January 1, January 1, 00:00:00 to the present, because the returned value is of the long type and can only be reached until January 1, 2038 int getFirstDayOfWeek (); // obtain the day of the week. Android123 prompts that the returned value is defined in java. util. in the Calendar class, such as Calendar. monday is Monday and the value is defined as 2. Long getMaxDate (); // obtain the maximum number of days from January 1, 1970 to that day long getMinDate (); // obtain the minimum number of days from January 1, 1970 to that day boolean getShowWeekNumber (); // obtain whether to display the day of the week boolean isEnabled (); // whether to display the current calendar view public void setDate (long date, boolean animate, boolean center ); // set the description void setDate (long date) from date to 1970; // set the description of the selected date to void setEnabled (boolean enabled) on 1970 ); // set whether to enable view void setFirstDayOfWeek (int firstDayOfWeek); // set the start day of the week Void setMaxDate (long maxDate); void setMinDate (long minDate); void setOnDateChangeListener (CalendarView. OnDateChangeListener listener); // The interface for modifying the calendar view is described below. Void setShowWeekNumber (boolean showWeekNumber); // you can specify whether to display the week number.

 

The setting interface for the calendar control CalendarView android. widget. CalendarView. OnDateChangeListener only implements a callback method void onSelectedDayChange (CalendarView view, int year, int month, int dayOfMonth ).

The four parameters are described as follows:

View: The CalendarView object.

Year: the year to be set.

Month: Specifies the month in the range of 0-11.

DayOfMonth: Specifies a day of each month.

Example:

Public class CalendarActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); CalendarView calendarView = (CalendarView) findViewById (R. id. calendarView1); calendarView. setOnDateChangeListener (new OnDateChangeListener () {@ Override public void onSelectedDayChange (CalendarView view, int year, int month, int dayOfMonth) {// TODO Auto-generated method stub String date = year + "year" + month + "month" + dayOfMonth + "day"; Toast. makeText (getApplicationContext (), date, 0 ). show ();}});}}

 

The implementation of the CalendarView control is as follows:

  

  

This is the calendar control that comes with Android and later versions. We can use the mobile mouse wheel to select the calendar control for the last month or the next month, and then select the date we want. The feeling is that the flexibility is not enough, for more information about CalendarView, see the official Android documentation: http://developer.android.com/reference/android/widget/calendarview.html.

 

Of course, some may develop in versions earlier than Android. If calendar controls are used in versions earlier than Android, the first solution is to use third-party controls, as described below, the second solution is to port the code above Android3.0 to the following versions.

 

Here are several other useful calendar controls:

 

This is the calendar view. You can flip pages last month and next month and set events. To open the calendar control interface, use the following method:

startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(null, CalendarActivity.MIME_TYPE));

 

If you want to click a date on the control and return the result, you can use the following method:

// Open the calendar control interface startActivityForResult (new Intent (Intent. ACTION_PICK ). setDataAndType (null, CalendarActivity. MIME_TYPE), 100); // rewrite the onActivityResult () method to process the returned date @ Override public void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = RESULT_ OK) {int year = data. getIntExtra ("year", 0); // get the year int month = data. getIntExtra ("month", 0); // obtain the monthly int day = data. getIntExtra ("day", 0); // get the day // format the date to display final Calendar dat = Calendar. getInstance (); dat. set (Calendar. YEAR, year); dat. set (Calendar. MONTH, month); dat. set (Calendar. DAY_OF_MONTH, day); // display the date result SimpleDateFormat format = new SimpleDateFormat ("yyyy MMM dd"); Toast. makeText (TestActivity. this, format. format (dat. getTime (), Toast. LENGTH_LONG ). show ();}}

 

: Click to download

 

 

This calendar control is also powerful, but it is not user-friendly. If you are interested or need it, you can modify it.

 

: Click to download

 

 

: Click to download

 

 

This is our own calendar control. You can select a date to query related data.

 

:/Files/hanyonglu/AndroidFile/MyCalendarDemo.rar

 

The above several useful calendar controls on the Android platform and the source code have been released.

 

Finally, I hope to reprint friends can respect the author's labor results, plus reprint address: http://www.cnblogs.com/hanyonglu/archive/2012/03/26/2418178.html thank you.
 

Complete. Pai_^

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.