Android Date and Time Selection control usage

Source: Internet
Author: User

Timepicker also inherits from the framelayout class. The Time Selection Control displays the time of a day (in 24 hours or am/pm format) to the user and allows the user to select. To capture the event of the time data modified by the user, you need to add the ontimechangedlistener listener for the timepicker.

Usage of the following Date and Time Selection Control: directory structure main. xml layout File
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<datepicker android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<edittext android:id="@+id/dateet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorvisible="false"
android:editable="false"/>
<timepicker android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<edittext android:id="@+id/timeet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorvisible="false"
android:editable="false"/>
</linearlayout>
Dptpactivity class
Package com. ljq. activity;

Import java. util. calendar;

Import android. app. activity;
Import android. OS. bundle;
Import android. widget. datepicker;
Import android. widget. edittext;
Import android. widget. timepicker;
Import android. widget. datepicker. ondatechangedlistener;
Import android. widget. timepicker. ontimechangedlistener;

Public class dptpactivity extends activity {
Private edittext dateet = null;
Private edittext timeet = null;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (r. layout. main );
Dateet = (edittext) findviewbyid (r. id. dateet );
Timeet = (edittext) findviewbyid (r. id. timeet );
Datepicker = (datepicker) findviewbyid (r. id. datepicker );
Timepicker = (timepicker) findviewbyid (r. id. timepicker );

Calendar = calendar. getinstance ();
Int year = calendar. get (calendar. year );
Int monthofyear = calendar. get (calendar. month );
Int dayofmonth = calendar. get (calendar. day_of_month );
Datepicker. init (year, monthofyear, dayofmonth, new ondatechangedlistener (){

Public void ondatechanged (datepicker view, int year,
Int monthofyear, int dayofmonth ){
Dateet. settext ("the date you selected is:" + year + "year" + (monthofyear + 1) + "month" + dayofmonth + "day. ");
}

});

Timepicker. setontimechangedlistener (new ontimechangedlistener (){

Public void ontimechanged (timepicker view, int hourofday, int minute ){
Timeet. settext ("the time you selected is:" + hourofday + "Hour" + minute +. ");
}

});
}
}
Running result

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.