Time-date selectors in Android

Source: Internet
Author: User

1.layout

<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 "> <Butt On android:id= "@+id/button01_time" android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "android:onclick=" Showtimepickerdialog "android:text=" @string/pick_time "/> <button an Droid:id= "@+id/btn_date" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and roid:layout_below= "@id/button01_time" android:onclick= "Showdatepickerdialog" android:text= "@string/pick_date "/> <textview android:id=" @+id/tv_settime "android:layout_width=" Wrap_content "Android:layo ut_height= "Wrap_content" android:layout_alignbaseline= "@+id/button01_time" android:layout_alignbottom= "@+id/ Button01_time "Android:layout_torightof= "@+id/button01_time"/> <textview android:id= "@+id/tv_setdate" android:layout_width= "wrap_content "Android:layout_height=" wrap_content "android:layout_alignbaseline=" @+id/btn_date "Android:layout_al Ignbottom= "@+id/btn_date" android:layout_torightof= "@+id/btn_date"/></relativelayout>

2.string.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >android_020</ string>    <string name= "action_settings" >Settings</string>    <string name= "Hello_world" >hello world!</string>    <string name= "Pick_time" > Time settings </string>    <string name= "Pick_ Date > Date settings </string>    <string name= "SetTime" > Time = = Settings </string>    <string name= "setdate "> Date = = Settings </string></resources>

3.activitymain.java

Package Com.xrhou.android_020;import Java.util.calendar;import Android.annotation.suppresslint;import Android.app.activity;import Android.app.datepickerdialog;import Android.app.DatePickerDialog.OnDateSetListener; Import Android.app.dialog;import Android.app.dialogfragment;import Android.app.timepickerdialog;import Android.app.timepickerdialog.ontimesetlistener;import Android.os.bundle;import Android.text.format.DateFormat; Import Android.view.menu;import android.view.view;import android.widget.datepicker;import Android.widget.TextView; Import android.widget.timepicker;/** * Time Date selection dialog box * * @author hbj403 */@SuppressLint ("Validfragment") public class Mainact Ivity extends Activity {private TextView tv_settime,tv_setdate; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tv_settime= ( TextView) Findviewbyid (r.id.tv_settime); tv_setdate= (TextView) Findviewbyid (r.id.tv_setdate);} @Overridepublic Boolean OncreateoptiOnsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} Time selector public void Showtimepickerdialog (view view) {Timedialogme tpf=new timedialogme (); Tpf.show (Getfragmentmanager ( ), "Showmytime");} Date selector public void Showdatepickerdialog (view view) {Datedialogme ddm=new Datedialogme ();dd m.show (Getfragmentmanager ( ), "Showmydatet");} Time Picker Class Timedialogme extends Dialogfragment implements Ontimesetlistener {@Overridepublic Dialog oncreatedialog ( Bundle savedinstancestate) {final Calendar calendar=calendar.getinstance (); int Hour=calendar.get (calendar.hour_of_ day); int minute=calendar.get (Calendar.minute); Timepickerdialog tpd=new Timepickerdialog (getactivity (), this, hour, minute, Dateformat.is24hourformat (GetActivity () ); return TPD;} @Overridepublic void Ontimeset (timepicker view, int hourofday, int minute) {Tv_settime.settext (hourofday+ ":" +minute);}} Date Selector Class Datedialogme extends Dialogfragment implements ondatesetlistener{@Overridepublic Dialog Oncreatedialog ( Bundle savedInstancestate) {final Calendar c=calendar.getinstance (); int year=c.get (calendar.year); int Month=c.get ( Calendar.month); The Calendar MONTH corresponds to 0-11int day=c.get (calendar.day_of_month);D atepickerdialog dpd=new Datepickerdialog ( Getactivity (), this, year, month, day); return DPD;} @Overridepublic void Ondateset (DatePicker view, int year, int monthofyear,int dayofmonth) {Tv_setdate.settext (year+ "Years" + (monthofyear+1) + "month" +dayofmonth+ "Day");}}

  

Time-date selectors in Android

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.