Date widget
DatePickerAndDatePickerDialog,DatePickerDialogYesDatePicker,. There is a trigger MethodOnDateChangedListener () and OnDateSetListener ().
In this example, we set twoButtonAnd oneTextViewWhen the button pops upDatePickDialog.
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)
Step 3: pop up the date Dialog and Set the Set trigger callback function.
Java code:
- New DatePickerDialog (
- /* Parameter 1: context. In my example, it is called in an internal class. All parameters that need to be specified as this */Chapter9Test1. this,
- /* Parameter 2: Set the Set date callback function */dateSet,
- /* Parameter 3, 4, 5: Set year, month, and day */calendar. get (Calendar. YEAR), calendar. get (Calendar. MONTH), calendar. get (Calendar. DATE )). show ();
The last show () indicates that the dialog is displayed. The Set callback function is OnDateSetListener (), as follows:
Java code:
- 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);
- }
- };
:
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:
Java code:
- <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
- <AnalogClock android:id="@+id/c91_analog"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_alignParentTop="true" />
- <DigitalClock android: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>
:
Insert a new Activity in Android Development
Use Internet Data in Android applications
The development of three major Android Manufacturers
The openness of the Android platform makes it increasingly favored by application developers.