Android Development Time Date 2

Source: Internet
Author: User
Tags event listener time and date

Yesterday to share a small example of time and date settings, today found that the Android layout component has a set of time and date components, no nonsense to say, first to share my experience.

 Time-Date Setup components: Timepicker, DatePicker

In the layout file can be added directly to our layout style, we look at the code:

<linearlayout         android:id= "@+id/linear1"        android:orientation= "vertical"        android:layout_width= " Fill_parent "        android:layout_height=" wrap_content "        >    <edittext        android:id=" @+id/editview "        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"/>    <linearlayout         android:id= "@+id/linear2"        android:orientation= "horizontal"        android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content"        >
Time Settings Component <timepicker android:id= "@+id/time" android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" />
Date settings Component <datepicker android:id= "@+id/date" android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" /> </LinearLayout></LinearLayout>

. java files:

public class Mainactivity extends Activity {private EditText myedit = null; 
Declare two components private timepicker time = null;private DatePicker date = null; @Overrideprotected void OnCreate (Bundle savedins Tancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);

//Set the display direction of activity to horizontal screen setrequestedorientation (activityinfo.screen_orientation_landscape); myedit = (EditText) fi Ndviewbyid (R.id.editview);
//Call two components in a layout file time = (Timepicker) Findviewbyid (r.id.time);d ate = (DatePicker) Findviewbyid (r.id.date);//time.se Tis24hourview (TRUE);//set with a 24-hour time display, the system default 12-hour Time.setontimechangedlistener (new MyTime ()); The
//note here is that the date does not have the Ondatechangedlistener () method, which we set here by calling the date's init () method. Date.init (This.date.getYear (), This.date.getMonth (), This.date.getDayOfMonth (), New MyDate ()); This.setdatetime ();}
Time Change Event Monitoring:

Class MyTime implements ontimechangedlistener{@Overridepublic void ontimechanged (timepicker view, int hourofday, int minute) {//TODO auto-generated method StubMainActivity.this.setDateTime ();}}
Date Change Event listener @suppresslint ("Newapi") class MyDate implements ondatechangedlistener{@Overridepublic void ondatechanged ( DatePicker View, int year, int monthofyear,int dayofmonth) {//TODO auto-generated method StubMainActivity.this.setDateTi Me ();}} public void SetDateTime () {//the contents of the text input component will also be modified after the date time change This.myedit.setText (this.date.getYear () + "-" + ( This.date.getMonth () + 1) + "-" + this.date.getDayOfMonth () + "" + this.time.getCurrentHour () + ":" + This.time.getCurrentM Inute ()); Modify the contents of the text}}

  

As follows:

 

Personal feeling in this example the more difficult point is that the date event does not have a Onchangedlistener () method, where we use the date of the Init () method instead. Next is how to control the activity display direction, activity display direction By default is vertical screen display, we can from the Java file from the new settings, can also be set in the configuration file, the personal feel in the Java file settings more flexible.

Content is not a difficult point, just to share with you, seek expert guidance.

Android Development Time Date 2

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.