Android Program Development: (11) Select the control -- 11.2 DatePicker

Source: Internet
Author: User

DataPicker is similar to the TimePicker mentioned in the previous section. DatePicker allows you to select a specific date. The following describes how to use DatePicker.

1. Use the project in the previous section, BasicViews4, and modify main. xml.

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<Button android: id = "@ + id/btnSet"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "I am all set! "
Android: onClick = "onClick"/>
 
<DatePicker android: id = "@ + id/datePicker"
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"/>
 
</LinearLayout>
2. F11 debugging. In portrait mode, the explicit DatePicker is slightly narrower. Press Ctrl + F11 to change the display direction of the screen.


3. Add some code in BasicViews4Activity. java.

[Java]
Package net. learn2develop. BasicViews4;
 
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;
 
Import android. app. Activity;
Import android. app. DatePickerDialog;
Import android. app. Dialog;
Import android. app. TimePickerDialog;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. DatePicker;
Import android. widget. TimePicker;
Import android. widget. Toast;
 
Public class BasicViews4Activity extends Activity {
TimePicker timePicker;
DatePicker datePicker;
 
Int hour, minute;
Int yr, month, day;
 
Static final int TIME_DIALOG_ID = 0;
Static final int DATE_DIALOG_ID = 1;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
TimePicker = (TimePicker) findViewById (R. id. timePicker );
TimePicker. setIs24HourView (true );
 
// ShowDialog (TIME_DIALOG_ID );
DatePicker = (DatePicker) findViewById (R. id. datePicker );
 
// --- Get the current date ---
Calendar today = Calendar. getInstance ();
Yr = today. get (Calendar. YEAR );
Month = today. get (Calendar. MONTH );
Day = today. get (Calendar. DAY_OF_MONTH );
 
ShowDialog (DATE_DIALOG_ID );
}
 
@ Override
Protected Dialog onCreateDialog (int id)
{
Switch (id ){
Case TIME_DIALOG_ID:
Return new TimePickerDialog (
This, mTimeSetListener, hour, minute, false );
Case DATE_DIALOG_ID:
Return new DatePickerDialog (
This, mDateSetListener, yr, month, day );
 
}
Return null;
}
 
Private DatePickerDialog. OnDateSetListener mDateSetListener =
New DatePickerDialog. OnDateSetListener ()
{
Public void onDateSet (
DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Yr = year;
Month = monthOfYear;
Day = dayOfMonth;
Toast. makeText (getBaseContext (),
"You have selected:" + (month + 1) +
"/" + Day + "/" + year,
Toast. LENGTH_SHORT). show ();
}
};
 
Private TimePickerDialog. OnTimeSetListener mTimeSetListener =
New TimePickerDialog. OnTimeSetListener ()
{
Public void onTimeSet (
TimePicker view, int hourOfDay, int minuteOfHour)
{
Hour = hourOfDay;
Minute = minuteOfHour;
 
SimpleDateFormat timeFormat = new SimpleDateFormat ("hh: mm aa ");
Date date = new Date (0, 0, 0, hour, minute );
String strDate = timeFormat. format (date );
 
Toast. makeText (getBaseContext (),
"You have selected" + strDate,
Toast. LENGTH_SHORT). show ();
}
};
 
Public void onClick (View view ){
Toast. makeText (getBaseContext (),
"Date selected:" + (datePicker. getMonth () + 1) +
"/" + DatePicker. getDayOfMonth () +
"/" + DatePicker. getYear () + "\ n" +
"Time selected:" + timePicker. getCurrentHour () +
":" + TimePicker. getCurrentMinute (),
Toast. LENGTH_SHORT). show ();
}
 
}

4. click the button.

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.