TimePicker and DatePicker for android control development
Development of android controls using TimerPicker and DatePicker
Java code:Package com. example. timepicker;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. app. DatePickerDialog;
Import android. app. Dialog;
Import android. app. TimePickerDialog;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. DatePicker;
Import android. widget. TimePicker;
Public class MainActivity extends Activity {
Private final int TIMER = 1;
Private final int DATE = 2;
Private Button showTimerButton = null;
Private Button showDateButton = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ShowTimerButton = (Button) findViewById (R. id. showTimePicker );
ShowTimerButton. setOnClickListener (new showTimerDialogListener ());
ShowDateButton = (Button) findViewById (R. id. showDatePicker );
ShowDateButton. setOnClickListener (new showDateDialogListener ());
}
// Date button listener
Class showDateDialogListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Display Date Diaolog
ShowDialog (DATE );
}
}
// Timer button listener
Class showTimerDialogListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Display the dialog whose ID is TIMER
Showdimer (TIMER );
// TimePickerDialog timerDialog = new TimePickerDialog (this, onTimeSetListener, 22, 54, true );
// TimerDialog. show ();
}
}
// This method is called by showDialog () and the Dialog ID is passed in.
Protected Dialog onCreateDialog (int id ){
Dialog dialog = null;
Switch (id ){
// When the ID is TIMER, create a TimePickerDialog object and return
Case TIMER:
Dialog = new TimePickerDialog (this, onTimeSetListener, 22, 54, true );
Break;
// When the ID is DATE, create a DatePickerDialog object and return
Case DATE:
Dialog = new DatePickerDialog (this, onDateSetListener, 2014, 12, 10 );
Break;
}
Return dialog;
}
The main. xml file is as follows:
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
Android: id = "@ + id/myText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
Android: id = "@ + id/showTimePicker"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/myText"
Android: text = "@ string/show_timer"/>
Android: id = "@ + id/showDatePicker"
Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent"
Android: layout_below = "@ id/showTimePicker"
Android: text = "@ string/show_date"/>
The Strings. xml Code is as follows:
PickerDialog
Settings
Hello world!
Show Time Dialog
Show Date Dialog
The following figure shows the result:
Click Show Timer Dialog as follows:
Click Show Date Dialog as follows: