TimePicker and DatePicker for android control development

Source: Internet
Author: User

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:


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.