Use DatePickerDialog and TimePickerDialog in Android

Source: Internet
Author: User

We want to achieve this effect, such:

 
 


Click set date and time:

 



Click settings to bring up the following content:

 
 

 

 

The last point is to display the date and time in the preceding text box.

 

 

 

Implementation Code:

Package org. crazyit. dialog;
Import java. util. Calendar;
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. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. DatePicker;
Import android. widget. EditText;
Import android. widget. TimePicker;
/**
* @ Author Wu rihui
*/
Public class DateDialog extends Activity {
// Used to splice the date and time, and finally used to display
StringBuilder str = new StringBuilder ("");
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button dateBn = (Button) findViewById (R. id. dateBn );
// Bind the listener to the "set date" button.
DateBn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View source ){
Calendar c = Calendar. getInstance ();
// Create a DatePickerDialog dialog box and display it
Dialog dateDialog = new DatePickerDialog (DateDialog. this,
// Bind the listener
New DatePickerDialog. OnDateSetListener (){
@ Override
Public void onDateSet (DatePicker dp, int year,
Int month, int dayOfMonth ){
Str. append (year + "-" + (month + 1) + "-"
+ DayOfMonth + "");
Calendar time = Calendar. getInstance ();
Dialog timeDialog = new TimePickerDialog (
DateDialog. this,
// Bind the listener
New TimePickerDialog. OnTimeSetListener (){
@ Override
Public void onTimeSet (
TimePicker tp,
Int hourOfDay, int minute ){
Str. append (hourOfDay + ":"
+ Minute );
EditText show = (EditText) findViewById (R. id. show );
Show. setText (str );
}
}
// Set the initial time
, Time. get (Calendar. HOUR_OF_DAY), time
. Get (Calendar. MINUTE)
// True indicates that the 24-hour format is used.
, True );
TimeDialog. setTitle ("select a date ");
TimeDialog. show ();


}
}
// Set the initial date
, C. get (Calendar. YEAR), c. get (Calendar. MONTH), c
. Get (Calendar. DAY_OF_MONTH ));
DateDialog. setTitle ("select a date ");
DateDialog. show ();
}
});
}
}


Xml 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"
Android: gravity = "center_horizontal"
>
<EditText
Android: id = "@ + id/show"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: editable = "false"
/>


<Button
Android: id = "@ + id/dateBn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "set date and time"
/>
</LinearLayout>


Conclusion: The code is not very difficult. As long as you read it carefully, I think you can understand it. What are you waiting for? Try it on your own!

 

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.