"Android Learning Notes" Datepickerdialog and Timepickerdialog date selector and time picker

Source: Internet
Author: User


(1) Layout file

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@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 "> <button android:id=" @+id/button2 "android:layout_width=" Wrap_content "Androi        d:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true"        android:layout_margintop= "156DP" android:text= "Time dialog box"/> <button android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@+id /button2 "android:layout_below=" @+id/button2 "        android:layout_margintop= "14DP" android:text= "Date dialog box"/> <edittext android:id= "@+id/edittext 1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignparen Ttop= "true" android:layout_margintop= "41DP" android:ems= "ten" > <requestfocus/> </edit Text></relativelayout>

(2) class file

Package Com.example.date_time_dailog;import Java.util.calendar;import Android.app.activity;import Android.app.datepickerdialog;import Android.app.datepickerdialog.ondatesetlistener;import Android.app.timepickerdialog;import Android.app.timepickerdialog.ontimesetlistener;import Android.os.Bundle; Import Android.view.menu;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;import Android.widget.toast;public class Mainactivity extends Activity {private Button Button1;private Button button2;private EditText edittext;private datepickerdialog datepickerdialog; Date dialog box private int year, monthofyear, DayOfMonth, Hourofday, minute;private timepickerdialog timepickerdialog;// Time dialog box @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) This.findviewbyid (R.id.buttOn1) Button2 = (Button) This.findviewbyid (r.id.button2); editText = (EditText) This.findviewbyid (R.ID.EDITTEXT1);/* * Gets the current date and time */calendar Calendar = Calendar.getinstance (); year = Calendar.get (calendar.year); monthofyear = Calendar.get ( Calendar.month) + 1;dayofmonth = Calendar.get (calendar.day_of_month); hourofday = Calendar.get (Calendar.HOUR_OF_DAY); minute = Calendar.get (calendar.minute);/* Set the date selector */datepickerdialog = new Datepickerdialog (This, new Ondatesetlistener () {@Overridepublic void Ondateset (DatePicker view, int year, int monthofyear,int dayofmonth) {String te XT = year + "-" + Monthofyear + "-" + dayofmonth;edittext.settext (text);}}, year, Monthofyear, DayOfMonth); Button1.setoncl Icklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {datepickerdialog.show ();}}); * * Time selector settings */timepickerdialog = new Timepickerdialog (mainactivity.this,new Ontimesetlistener () {@Overridepublic void Ontimeset (timepicker view, int hourofday,int minute) {String text = Hourofday+ ":" + minute;edittext.settext (text),}}, Hourofday, minute, true); Button2.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {timepickerdialog.show ();}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

(3) Operation result




"Android Learning Notes" Datepickerdialog and Timepickerdialog date selector and time picker

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.