Android Learning Note (21): Interesting widget-date and time

Source: Internet
Author: User

Android provides the widget for the date and time.

Date Widget

DatePicker and Datepickerdialog,datepickerdialog are a simple container for loading datepicker. There is a trigger method Ondatechangedlistener () and Ondatesetlistener () respectively.

In this example, we set up two buttons and a textview when the button pops up Datepickdialog.

Step 1: Some Java functions about time

    • Get an instance of the current time: Calendar calendar = Calendar.getinstance ();
    • Get current time: Calendar.get (Calendar.year), by setting parameters can be obtained year, month, day, hour, minute, second
    • Set time: Calendar.set (calendar.year,2011), can be set year, month, day, hour, minute, second
    • Using string to give the current time information, you can use the Java SimpleDateFormat, the following processing:
      SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm"); You can set different types
      The relevant info string can be obtained by Sdf.format (Calendar.gettime ().

Step 2: Set up the Android XML file and write the relevant code (omitted)

Step 3: Pop-up Date dialog, and set the trigger callback function for the set

New Datepickerdialog (
/* parameter 1:context, in my example is called in the inner class, all need to indicate that this is the this*/chapter9test1.this,
/* Parameter 2: Sets the callback function for the set date */Dateset,
/* Parameter 3,4,5: Day of the Month/Calendar.get (calendar.year), Calendar.get (Calendar.month), Calendar.get (Calendar.date)). Show ();

The last Show () indicates that the dialog is displayed. The callback function for set is Ondatesetlistener (), as follows:

datepickerdialog.ondatesetlistener dateset = new Datepickerdialog.ondatesetlistener () {
public void Ondateset(DatePicker view, int year, int monthofyear, int dayofmonth) {
Calendar.set (Calendar.year, year);
Calendar.set (Calendar.month, monthofyear);
Calendar.set (Calendar.date, DayOfMonth);
}
};

Time Widgets

The date is very similar, and timepickerdialog (which can be set by Ontimesetlistener) is a simple dialog container that loads Timepicker (which can be set by the Ontimechangedlistener callback function).

Pop-up dialog, can follow the picture scoop, the following way. The same first argument is the context, the second parameter is the callback function, the third and the four are the time and the minute, and the 5th parameter indicates whether the 24-hour system is displayed.

New Timepickerdialog (Chapter9test1.this, Timeset, Calendar.get (Calendar.hour_of_day), Calendar.get (Calendar.MINUTE ), true). Show ();

For the callback function Timeset, the following settings are available:

timepickerdialog.ontimesetlistener timeset = new Timepickerdialog.ontimesetlistener () {
@Override
public void Ontimeset(timepicker view, int hourofday, int minute) {
Calendar.set (Calendar.hour_of_day, hourofday);
Calendar.set (Calendar.minute,minute);
}
};

Other ways to tune up dialog in activity

In the example above, the Xxxpickerdialog is created and the show () method is used to tune them up. In the case of Android tutorial, you can rewrite the Acitivity Oncreatedialog method to implement, below with the date of the dialog as an example.

public class Chapter9tutorial1extends Activity{
... ...
Step 1: Give the IDs of the different dialog we need to display so that we can determine which dialog
static final int date_dialog_id = 0;
Step 2: Adjust the corresponding Dialog by ShowDialog (Dialog ID)
    protected void SomeMethod (...) {
... ...
ShowDialog (date_dialog_id);
... ...
}
Override acitivity method Oncreatedialog (int id), which tells ShowDialog () which dialog to display, giving dialog objects
protected Dialog oncreatedialog (int id) {
Switch (ID) {
CaseDATE_DIALOG_ID:
return new Datepickerdialog (
This,mdatesetlisten,myear,mmonth,mday);
... ...
}
return Super.oncreatedialog (ID);or return null directly;
}
}

Analog Clocks and digital clocks

In the previous example, we usually set a date or time, and if we just want to show the current time to the user, we can use the analog always and the digital clock. , the following is the relevant Android XML file:

<relativelayout android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "Fill_ Parent ">
<AnalogClockAndroid:id= "@+id/c91_analog"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
Android:layout_alignparenttop= "true"/>
<DigitalClockAndroid:id= "@+id/c91_digital"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
android:layout_below= "@id/c91_analog"/>
</RelativeLayout>

Timer chronometer

Android provides a timer chronometer, a widget similar to Diagitalclock, defined in XML by using the <chronometer .../> . Here's an example from Android reference. The top is the chronometer widget.

There are several key control functions:

    • Start (): Start real-time update timer
    • Stop (): Stop the real-time update timer, note that the timer is actually still walking
    • SetBase (): Sets the initial base time, if the current time is set, SetBase (Systemclock.elapsedrealtime ()), which resets the timer, the timer is zeroed
    • Format (): Set the display format, default to Mm:ss or H:MM:SS, if we want to display the content is "my timer 00:00", you can use format ("My timer%s"), the first%s, representing the default time format. If the format is emptied, it is implemented by default, as seen by format (NULL).

Grade Badminton

Saturday when the unit network published this year's badminton competition, and the usual and the same, this time the abolition of the singles, increased doubles, not men's doubles, not doubles, not mixed doubles, is the right double. Must be a level three department leader above, must one of them is the level two department leader, Gender Unlimited. That is to say, now the players are divided ranked, as for the "status" of the equivalent, only qualified to stand on the same court. It seems that India's caste is doing something similar. There are different division of labor in the crowd, some people's ability is stronger, the responsibility is capitalized, the remuneration is also different, but there is no grade in the crowd, the birth does not bring death. On the pitch, there are always too many things out there, like football.

RELATED links: My Andriod development related articles

Android Learning Note (21): Interesting widget-date and time

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.