Simple use of Timepicker and DatePicker in Android

Source: Internet
Author: User

Package com.example.demo10;

Import Java.util.Calendar;

Import android.support.v7.app.ActionBarActivity;
Import Android.app.DatePickerDialog;
Import Android.app.DatePickerDialog.OnDateSetListener;
Import Android.app.TimePickerDialog;
Import Android.app.TimePickerDialog.OnTimeSetListener;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.DatePicker;
Import Android.widget.DatePicker.OnDateChangedListener;
Import Android.widget.TimePicker;
Import Android.widget.TimePicker.OnTimeChangedListener;


public class Mainactivity extends Actionbaractivity {

Private Timepicker Timepicker;
Private DatePicker DatePicker;

Private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get the object for the calendar
Cal=calendar.getinstance ();
Get the corresponding month-day time and minute
Year=cal.get (calendar.year);
Month=cal.get (calendar.month) +1;
Day=cal.get (Calendar.day_of_month);
Hour=cal.get (Calendar.hour_of_day);
Minute=cal.get (Calendar.minute);

Settitle (year+ "-" +month+ "-" +day+ "-" +hour+ ":" +minute ");

Datepicker= (DatePicker) Findviewbyid (R.id.datepicker);
Timepicker= (Timepicker) Findviewbyid (R.id.timepicker);

DatePicker initialization
Datepicker.init (Year,cal.get (calendar.month), Day,new Ondatechangedlistener () {

@Override
public void ondatechanged (DatePicker view, int year, int monthofyear,
int DayOfMonth) {
TODO auto-generated Method Stub
Settitle (year+ "-" + (monthofyear+1) + "-" +dayofmonth);
}
});

Timepicker.setontimechangedlistener (New Ontimechangedlistener () {

@Override
public void ontimechanged (timepicker view, int hourofday, int minute) {
TODO auto-generated Method Stub
Settitle (hourofday+ ":" +minute);
}
});

New Datepickerdialog (This,new Ondatesetlistener () {

@Override
public void Ondateset (DatePicker view, int year, int monthofyear,
int DayOfMonth) {
TODO auto-generated Method Stub
Settitle (year+ "-" + (monthofyear+1) + "-" +dayofmonth);
}
}, year, Cal.get (Calendar.month), day). Show ();

New Timepickerdialog (This,new Ontimesetlistener () {

@Override
public void Ontimeset (timepicker view, int hourofday, int minute) {
TODO auto-generated Method Stub
Settitle (hourofday+ ":" +minute);
}
}, hour, minute,true). Show ();
}



}

Simple use of Timepicker and DatePicker in Android

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.