"Turn" Pro Android Learning Note (17): User interface and Control (5): Date and Time control

Source: Internet
Author: User

Directory (?) [-]

    1. DatePicker and Timepicker controls
    2. DigitalClock and AnalogClock controls
DatePicker and Timepicker controls

Using DatePicker and Timepicker, the name Incredibles, the user can choose the time. The XML code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout. >
<textview ... android:id= "@+id/ui_date1"/>
<DatePicker android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/ui_datepick"/>

<textview ... android:id= "@+id/ui_time1"/>
<timepicker android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/ui_timepick"/>
</LinearLayout>

If we do not set the date and time values in the code, initially, Datepiker and Timepiker will be set to the current date and time. For both of these controls, in code, we can read and write them. For dates are:

TextView Datetext = (TextView) Findviewbyid (r.id.ui_date1);
Datetext.settext ("Set default date to 2013/1/1");
DatePicker DP = (DatePicker) Findviewbyid (R.id.ui_datepick);
//To set the DatePicker, use theDp.init (year, Monthofyear, DayOfMonth, Ondatechangedlistener), it should be noted that the valid value of the month is "0-11", for example Dp.init (1, 1, NULL); The setting is 2013.2.1.
Dp.init (2013,0,1,new datepicker.ondatechangedlistener () {
Triggering of date changes by Ondatechangedlistener
public voidondatechanged(DatePicker view, int year, int monthofyear, int dayofmonth) {
Note: It will be triggered two times. Since picker actually contains two parts of the calendar and date carousel (which automatically syncs), the calendar and date changes produce two triggers. But calendar changes can automatically change the date, that is, the date changes through the calendar are triggered only once.
LOG.D ("Uidatetimetest", "set Date to" + year+ "." + (monthofyear+1) + "." +dayofmonth);

}
});
Dp.Setcalendarviewshown(false); Do not display the Calendar section, only the date Carousel section is displayed

For time:

Setting the processing time changes
timepicker.ontimechangedlistener Timechangedfunc = new Timepicker.ontimechangedlistener () {
@Override
public void ontimechanged(timepicker view, int hourofday, int minute) {
LOG.D ("Uidatetimetest", "Set time to" +hourofday + ":" + minute);
}
};

TextView timetext = (TextView) Findviewbyid (r.id.ui_time1);
Timetext.settext ("Set Default time to 16:00");
Timepicker TP = (timepicker) Findviewbyid (R.id.ui_timepick);
TP.Setis24hourview(true);
TP.Setcurrenthour(16);
TP.Setcurrentminute(0);
TP.Setontimechangedlistener(Timechangedfunc); When the anonymous class is used here, Eclipse displays an error, so this example directly uses the object

DigitalClock and AnalogClock controls

Digital clock and analog clock. Unlike other spaces, we cannot modify the time it displays, and they will show the real time of the system. One convenient way to use them is that we don't need to write code, and they can be automatically synchronized and updated in time. The XML is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout. >
<digitalclock android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<analogclock android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (17): User interface and Control (5): Date and Time control

Related Article

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.