Exploration of DatePicker and TimePicker in Android Control

Source: Internet
Author: User

1. DatePicker inherits from the FrameLayout class. The date Selection Control provides users with date data including year, month, and day, and allows users to modify the data. To capture Data Events in the user's date Selection control, you need to add an OnDateChangedListener listener for DatePicker.

Ii. 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 controls

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 = (DatePicker) findViewById (R. id. datePicker );
TimePicker timePicker = (TimePicker) findViewById (R. id. timePicker );

Calendar 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.