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!