Usage of DatePicker and TimePicker in Android (including DatePickerDialog and TimePickerDialog) and custom picker in android

Source: Internet
Author: User

Usage of DatePicker and TimePicker in Android (including DatePickerDialog and TimePickerDialog) and custom picker in android

Effect: display the date and time modified by DatePicker and TimePicker on the program title bar in real time.

1. Implemented through DatePicker and TimePicker

The layout is main. xml.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <DatePicker        android:id="@+id/datePicker"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TimePicker        android:id="@+id/timePicker"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

MainActivity. java

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, month, day; private int hour, minute; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain an object of the Calendar. cal = Calendar. getInstance (); // obtain the year, month, day, hour, minute, and second Information year = cal. get (Calendar. YEAR); // month starts from 0 (January month = 0) 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); // set the title 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) {// monthOfYear calculates setTitle from 0 (year + "-" + (monthOfYear + 1) + "-" + dayOfMonth) ;}}); timePicker. setOnTimeChangedListener (new OnTimeChangedListener () {@ Override public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {setTitle (hourOfDay + ":" + minute );}});
}
}

First, create a Calendar Object to obtain the current system time and date, and then set it on the program title bar. Pay attention to cal. get (Calendar. the return value starts from 0, that is, the return value of the 0 era table January.

DatePicker initialization calls public void init (int year, int monthOfYear, int dayOfMonth, OnDateChangedListenerOnDateChangedListener). The last parameter is the listener of DatePicker. TimePicker directly sets the listener.

Run

Change DatePicker

Change TimePicker

2. Use Dialog to make it more beautiful

The layout is main. xml.

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" ></LinearLayout>

 

No controls added

 

MainActivity. java

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 Calendar cal; private int year, month, day; private int hour, minute; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain an object of the Calendar. cal = Calendar. getInstance (); // obtain the year, month, day, hour, minute, and second Information year = cal. get (Calendar. YEAR); // month starts from 0 (January month = 0) 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); // set the title setTitle (year + "-" + month + "-" + day + "" + hour + ":" + minute ); // form of the calendar selector dialog box new DatePickerDialog (this, new OnDateSetListener () {@ Override public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {setTitle (year + "-" + (monthOfYear + 1) + "-" + dayOfMonth) ;}, year, cal. get (Calendar. MONTH), day ). show (); // time selector dialog box (whether the last is24HourView parameter is in the 24-hour format) new TimePickerDialog (this, new OnTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {setTitle (hourOfDay + ":" + minute) ;}, hour, minute, true ). show ();}}

Create the DatePickerDialog and TimePickerDialog objects directly using the Anonymous class. The constructor is as follows:

Public DatePickerDialog (Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

Public TimePickerDialog (Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)

Directly add listeners using anonymous internal class methods in the constructor. is24HourView is a boolean value to indicate whether to use the 24-hour system.

 

Run

Summary:

Use cal. get (Calendar. the value obtained by MONTH is 1 less than the actual MONTH value. You need to add 1 to setTitle, and the initialization method passed to DatePicker and the construction method of DatePickerDialog should be passed in directly.

We recommend that you directly write cal. get (Calendar. MONTH) in these two methods. Do not use the custom variable month.

 

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.