Android provides widgets for date and time.
Date widget
Datepicker and datepickerdialog. datepickerdialog is a simple container for loading datepicker ,. There is a trigger method ondatechangedlistener () and ondatesetlistener () respectively ().
In this example, we set two buttons and a textview. When the button is clicked, The datepickdialog is displayed.
Step 1: some java functions related to time
- Instance for obtaining the current time: Calendar calendar = Calendar. getInstance ();
- Obtain the current time: calendar. get (Calendar. YEAR). You can obtain the YEAR, month, day, hour, minute, and second by setting parameters.
- Set Time: calendar. set (Calendar. YEAR, 2011). You can set YEAR, month, day, hour, minute, and second.
- String is used to provide the current time information. Java SimpleDateFormat can be used for processing as follows:
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm ");
// You can set different types
You can use sdf. format (calendar. getTime () to obtain the related info string.
Step 2: Set the android XML file and write the relevant code (omitted)
Step 3: pop up the date Dialog and Set the Set trigger callback function.
New DatePickerDialog (
/* Parameter 1: context. In my example, it is called in an internal class. All the parameters that need to be specified as this */
Chapter9Test1. this,
/* Parameter 2: Set the Set date callback function */dateSet,
/* Parameters 3, 4, 5: Set YEAR, MONTH, and day */calendar. get (Calendar. YEAR), calendar. get (Calendar. MONTH), and calendar. get (Calendar. DATE )).Show ();
The last show () indicates that the dialog is displayed. The Set callback function is ondatesetlistener (), as follows:
DatePickerDialog. OnDateSetListenerDateSet = new DatePickerDialog. OnDateSetListener (){
Public voidOnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth ){
Calendar. set (Calendar. YEAR, year );
Calendar. set (Calendar. MONTH, monthOfYear );
Calendar. set (Calendar. DATE, dayOfMonth );
}
};
Time widget
The date is very similar. timepickerdialog (the callback function can be set through ontimesetlistener) is a simple dialog container that loads timepicker (the callback function can be set through ontimechangedlistener.
The dialog box is displayed. You can use the following method to draw images. The same first parameter is context, the second parameter is the callback function, and the third and fourth parameters are time and minute. The third parameter indicates whether the display mode is in the 24-hour format.
New TimePickerDialog (Chapter9Test1. this, timeSet, calendar. get (Calendar. HOUR_OF_DAY), calendar. get (Calendar. MINUTE), true ).Show ();
For the callback function timeset, you can set it as follows:
TimePickerDialog. OnTimeSetListenerTimeset = new timepickerdialog. ontimesetlistener (){
@ Override
Public voidOnTimeSet(Timepicker view, int hourofday, int minute ){
Calendar. Set (calendar. hour_of_day, hourofday );
Calendar. Set (calendar. Minute, minute );
}
};
Other Dialog calling methods in Activity
In the preceding example, xxxpickerdialog is created and called using the show () method. In the Tutorial example of Android, you can rewrite the oncreatedialog method of acitivity to implement it. The following uses the dialog of the start date as an example.
Public class Chapter9Tutorial1Extends Activity{
......
// Step 1: Provide the IDs of the different Dialog s to be displayed to determine which Dialog s to be displayed.
Static final int DATE_DIALOG_ID = 0;
// Step 2: Call the corresponding Dialog through showDialog (Dialog ID)
Protected void someMethod (....){
......
ShowDialog (DATE_DIALOG_ID );
......
}
// Override the Acitivity method onCreateDialog (int id). This method tells showDialog () which Dialog is to be displayed and the Dialog object is given.
Protected Dialog onCreateDialog (int id ){
Switch (id ){
CaseDATE_DIALOG_ID:
Return new DatePickerDialog (This, mDateSetlisten, mYear, mMonth, mDay);
......
}
Return super. onCreateDialog (id );
// Or directly return null;
}
}
Analog clock and digital clock
In the previous example, we usually need to set a date or time. If we only want to display the current time to the user, we can use the analog always and digital clock ., The android XML file is as follows:
<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, which is similar to diagitalclock.<Chronometer.../>. The following is 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 still running.
- Setbase (): sets the initial benchmark time. If the current time is set, setBase (SystemClock. elapsedRealtime () resets the timer and returns to zero.
- Format (): sets the display format. The default value is MM: SS or H: MM: SS. If we want to display the content "my timer ", you can use format ("My Timer % s"). The first % s represents the default time format. If the format is cleared, the default format is used. format (null) is used.
Grade badminton
On Saturday, the organization Network announced the competition for this year's Badminton Competition. As usual, the Women's Singles were canceled this time and doubles were added, not the men's double, not the women's double, not the mixed double, but the right double. It must be a third-level department leader or above, and one of them must be a second-level department leader. The gender is not limited. That is to say, the current players are classified into three or six players. As for the "status", only qualified players can stand on the same stadium. It seems that India's castes are doing something similar. There are different division of labor in the group. Some people have stronger abilities, upper-case responsibilities, and different payment options. However, there is no Level in the group, so it will not lead to death. On the pitch, there are always too many off-site events, just like football.
Related Links: My Andriod development articles