Android UI time and date controls

Source: Internet
Author: User
Tags time and date

Package com. android. liu. widget;

Import java. util. Calendar;

Import android. app. Activity;
Import android. app. DatePickerDialog;
Import android. app. TimePickerDialog;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. DatePicker;
Import android. widget. TimePicker;
Import android. widget. DatePicker. OnDateChangedListener;

Import com. android. liu. R;

Public class DateTimeActivity extends Activity {
Button setData, setTime;
DatePicker dp;
TimePicker tp;
Calendar calendar;

@ Override
Public void onCreate (Bundle inStateInstance ){
Super. onCreate (inStateInstance );
SetContentView (R. layout. datatime );

Calendar = Calendar. getInstance ();

SetData = (Button) this. findViewById (R. id. setData );
SetTime = (Button) this. findViewById (R. id. setTime );

Dp = (DatePicker) this. findViewById (R. id. datapicket );
Tp = (TimePicker) this. findViewById (R. id. timepicker );

Dp. init (calendar. get (Calendar. YEAR), calendar. get (Calendar. MONTH), calendar. get (Calendar. DAY_OF_MONTH), new OnDateChangedListener (){

@ Override
Public void onDateChanged (DatePicker arg0, int year, int month,
Int day ){
// TODO Auto-generated method stub
// Process the changes here
// Calendar. set (year, month, day );

}});

Tp. setIs24HourView (true );

Tp. setOnTimeChangedListener (new TimePicker. OnTimeChangedListener (){

@ Override
Public void onTimeChanged (TimePicker arg0, int arg1, int arg2 ){
// TODO Auto-generated method stub

// Process the changes here
// Calendar. set (year, month, day, hourOfDay, minute );
}
});



// Set the date
SetData. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View arg0 ){
New DatePickerDialog (DateTimeActivity. this, new DatePickerDialog. OnDateSetListener (){

@ Override
Public void onDateSet (DatePicker arg0, int year, int month, int dayofmonth ){

// Set the calendar
}
}, Calendar. get (Calendar. YEAR), calendar. get (Calendar. MONTH), calendar. get (Calendar. DAY_OF_MONTH). show ();

}});


// Set the time
SetTime. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View arg0 ){

New TimePickerDialog (DateTimeActivity. this, new TimePickerDialog. OnTimeSetListener (){

@ Override
Public void onTimeSet (TimePicker view, int hourofday, int minu ){

// Set the time
}
}, Calendar. get (Calendar. HOUR_OF_DAY), calendar. get (Calendar. MINUTE), true). show ();

}});

}
}

 

 

 

<? 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">
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>

<DatePicker android: id = "@ + id/datapicket"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>

<TimePicker android: id = "@ + id/timepicker"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>

<Button android: id = "@ + id/setData" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "set date"/>

<Button android: id = "@ + id/setTime" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "Set Time"/>
</LinearLayout>

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.