Android: simple Widgets

Source: Internet
Author: User

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:

 
 
  1. New DatePickerDialog (
  2. /* Parameter 1: context. In my example, it is called in an internal class. All parameters that need to be specified as this */Chapter9Test1. this,
  3. /* Parameter 2: Set the Set date callback function */dateSet,
  4. /* 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:

 
 
  1. DatePickerDialog.OnDateSetListener dateSet = new DatePickerDialog.OnDateSetListener() {  
  2. public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {  
  3. calendar.set(Calendar.YEAR, year);  
  4. calendar.set(Calendar.MONTH, monthOfYear);  
  5. calendar.set(Calendar.DATE, dayOfMonth);   
  6.   }  
  7. }; 

:

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:

 
 
  1. <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">   
  2. <AnalogClock android:id="@+id/c91_analog" 
  3. android:layout_width="fill_parent" android:layout_height="wrap_content"   
  4. android:layout_centerHorizontal="true" 
  5. android:layout_alignParentTop="true" /> 
  6. <DigitalClock android:id="@+id/c91_digital" 
  7. android:layout_width="wrap_content" android:layout_height="wrap_content"   
  8. android:layout_centerHorizontal="true" 
  9. android:layout_below="@id/c91_analog" /> 
  10. </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.

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.