Android Date and Time selection dialog box Usage Example analysis _android

Source: Internet
Author: User

This example describes the use of the Android Date and Time selection dialog box. Share to everyone for your reference. Specifically as follows:

Manifest file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 package=" Com.ljq.dialog "
 android:versioncode=" 1 "
 android:versionname=" 1.0 ">
 < Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name=
 ". Alertdialog "
   android:label=" @string/app_name ">
  <intent-filter>
  <action android:name=" Android.intent.action.MAIN "/>
  <category android:name=" Android.intent.category.LAUNCHER "/>"
  </intent-filter>
 </activity>
 </application>
 <uses-sdk android:minsdkversion = "7"/>
 <uses-permission android:name= "Android.permission.WRITE_CALENDAR"/>
</manifest> 

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id= "@+id/linearlayout01" android:layout_width = "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" xmlns:android= "http://"
 Schemas.android.com/apk/res/android "> <edittext android:id=" @+id/et "android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:editable= "false" android:cursorvisible= "false"/> <Button android:text= Date Dialog "android:id=" @+id/datebtn "android:layout_width=" fill_parent "android:layout_height=" Wrap_ Content "/> <button android:text=" Time Dialog "android:id=" @+id/timebtn "android:layout_width=" Fill_parent "Android: layout_height= "Wrap_content"/> <digitalclock android:text= "@+id/digitalclock" android:textSize= "20dip" Andro id:gravity= "center" android:id= "@+id/digitalclock01" android:layout_width= "Fill_parent" android:layout_height= " Wrap_content "/> <analogclock android:id=" @+id/analogclock "Android:g"ravity= "center" android:layout_width= "fill_parent" android:layout_height= "wrap_content"/> </LinearLayout >

Alertactivity class:

Package com.ljq.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.widget.Button;
Import Android.widget.DatePicker;
Import Android.widget.EditText;
Import Android.widget.TimePicker;
 public class Alertdialog extends activity {private Button datebtn = null;
 Private Button timebtn = null;
 Private EditText Et=null;
 Private final static int date_dialog = 0;
 Private final static int time_dialog = 1;
 Private Calendar c = null;
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 et= (EditText) Findviewbyid (r.id.et);
 DATEBTN = (Button) Findviewbyid (R.ID.DATEBTN);
 TIMEBTN = (Button) Findviewbyid (R.ID.TIMEBTN);
  Datebtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {ShowDialog (date_dialog); }
 });
 Timebtn.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {ShowDialog (time_dialog);
 }
 });
 /** * Create date and Time selection dialog box * * * @Override protected Dialog oncreatedialog (int id) {Dialog Dialog = null;
  Switch (ID) {Case date_dialog:c = calendar.getinstance (); dialog = new Datepickerdialog (This, new Datepickerdialog.ondatesetlistener () {public void Ondateset (DatePicker dp
   , int year,int month, int dayofmonth) {Et.settext ("you selected:" + year + "years" + (month+1) + "Month" + DayOfMonth + "Day");
  }, C.get (calendar.year),//Incoming year c.get (calendar.month),//Incoming month c.get (calendar.day_of_month)//Incoming days);
 Break
  Case Time_dialog:c=calendar.getinstance (); Dialog=new Timepickerdialog (This, new Timepickerdialog.ontimesetlistener () {public void Ontimeset (Timepicker view
   , int hourofday, int minute) {Et.settext ("you selected:" +hourofday+ "+minute+");
  }, C.get (Calendar.hour_of_day), C.get (Calendar.minute), false); Break
 return dialog;

 }
}

Run Result:

I hope this article will help you with your Android program.

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.