Android Development: Google Date time selector

Source: Internet
Author: User
Tags datetime trim

Here to paste a demo source, share:

The code is as follows Copy Code

Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.TextView;
Import Com.bigkoo.pickerview.OptionsPopupWindow;
Import Com.bigkoo.pickerview.TimePopupWindow;
Import Com.bigkoo.pickerview.TimePopupWindow.OnTimeSelectListener;
Import Com.bigkoo.pickerview.TimePopupWindow.Type;
public class Mainactivity extends activity {

Private TextView tvtime, tvoptions;
Timepopupwindow Pwtime;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Tvtime= (TextView) Findviewbyid (r.id.tvtime);
Time Picker
Pwtime = new Timepopupwindow (this, type.all);
Callback After Time selection
Pwtime.setontimeselectlistener (New Ontimeselectlistener () {

@Override
public void Ontimeselect (date date) {
Tvtime.settext (GetTime (date));
}
});
Popup Time Picker
Tvtime.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Pwtime.showatlocation (tvtime, Gravity.bottom, 0, 0,new Date ());
}
});

}

public static String GetTime (date date) {
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm");
return Format.format (date);
}
}

Of course, only the above one source file is certainly not, share the two demo project address, we can go to see.

Http://itlanbao.com/code/users/10000/20150911/Android-PickerView-master.rar

Example 2

The code is as follows Copy Code

Customize a class:
/datetimepicker/src/com/wwj/datetimepicker/datetimepickdialogutil.java

Package com.wwj.datetimepicker;

Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.widget.DatePicker;
Import Android.widget.DatePicker.OnDateChangedListener;
Import Android.widget.EditText;
Import Android.widget.LinearLayout;
Import Android.widget.TimePicker;
Import Android.widget.TimePicker.OnTimeChangedListener;

/**
* Date Time Selection control use method: Private EditText inputdate;//The date time text edit box you want to set private String
* initdatetime= "September 3, 2012 14:44",//The initial date time value is used in the Click event:
* Inputdate.setonclicklistener (new Onclicklistener () {
*
* @Override public void OnClick (View v) {Datetimepickdialogutil
* Datetimepickdialog=new
* Datetimepickdialogutil (Sinvestigateactivity.this,initdatetime);
* Datetimepickdialog.datetimepickdialog (Inputdate);
*
*           } });
*
* @author
*/
public class Datetimepickdialogutil implements Ondatechangedlistener,
Ontimechangedlistener {
Private DatePicker DatePicker;
Private Timepicker Timepicker;
Private Alertdialog AD;
Private String DateTime;
Private String Initdatetime;
activity of private activity;

/**
* Date Time Popup selection box constructor
*
* @param activity
*: Invoked parent activity
* @param initdatetime
* Initial date time value, as popup title and date Time initial value
*/
Public Datetimepickdialogutil (activity activity, String Initdatetime) {
This.activity = activity;
This.initdatetime = Initdatetime;

}

 public void Init (DatePicker datepicker, Timepicker timepicker) {
  calendar Calendar = Calendar.getinstance ();
  if (!) ( Null = = Initdatetime | | '. Equals (Initdatetime))} {
   calendar = This.getcalendarbyinintdata (initdatetime);
 &NBSP} else {
   initdatetime = Calendar.get (calendar.year) + year
      + Calendar.get (calendar.month) + "month"
     + Calendar.get (Calendar.DAY_OF_ MONTH) + "Day"
     + calendar.get (calendar.hour_of_day) + ":"
      + Calendar.get (Calendar.minute);
  }

  datepicker.init (Calendar.get (calendar.year),
    calendar.get ( Calendar.month),
    calendar.get (calendar.day_of_month), this);
  timepicker.setcurrenthour (Calendar.get (calendar.hour_of_day));
  timepicker.setcurrentminute (Calendar.get (Calendar.minute));
 }

 /**
  * Popup Date Time selection box method
  *
  * @param inputdate
  *    &nbs p;      :
  * @return
  */
 public for date-time text edit boxes that need to be set Alertdialog Datetimepickdialog (Final edittext inputdate) {
  linearlayout datetimelayout = (LinearLayout Activity
    .getlayoutinflater (). Inflate (r.layout.common_datetime, null);
   datepicker = (DatePicker) Datetimelayout.findviewbyid (R.id.datepicker);
  timepicker = (timepicker) Datetimelayout.findviewbyid (R.id.timepicker);
  init (DatePicker, Timepicker);
  timepicker.setis24hourview (TRUE);
  timepicker.setontimechangedlistener (this);

AD = new Alertdialog.builder (activity)
. Settitle (Initdatetime)
. Setview (Datetimelayout)
. Setpositivebutton ("Settings", new Dialoginterface.onclicklistener () {
public void OnClick (dialoginterface dialog, int whichbutton) {
Inputdate.settext (DateTime);
}
})
. Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
public void OnClick (dialoginterface dialog, int whichbutton) {
Inputdate.settext ("");
}
). Show ();

ondatechanged (null, 0, 0, 0);
return AD;
}

public void ontimechanged (timepicker view, int hourofday, int minute) {
ondatechanged (null, 0, 0, 0);
}

public void ondatechanged (DatePicker view, int year, int monthofyear,
int DayOfMonth) {
Get Calendar Instance
Calendar calendar = Calendar.getinstance ();

Calendar.set (Datepicker.getyear (), Datepicker.getmonth (),
Datepicker.getdayofmonth (), Timepicker.getcurrenthour (),
Timepicker.getcurrentminute ());
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day hh:mm");

DateTime = Sdf.format (Calendar.gettime ());
Ad.settitle (DateTime);
}

/**
* Implementation of the original date time July 02, 2012 16:45 split into minutes and seconds, and assigned to calendar
*
* @param initdatetime
* Initial Date time value string type
* @return Calendar
*/
Private Calendar Getcalendarbyinintdata (String initdatetime) {
Calendar calendar = Calendar.getinstance ();

Split the initial date time July 02, 2012 16:45 into the day and the month
String date = splitestring (Initdatetime, "Day", "index", "front"); Date
String time = splitestring (Initdatetime, "Day", "index", "back"); Time

String yearstr = splitestring (date, "year", "Index", "front"); Year
String Monthandday = splitestring (date, "year", "Index", "back"); Month Day

String monthstr = splitestring (Monthandday, "month", "index", "front"); Month
String daystr = splitestring (Monthandday, "month", "index", "back"); Day

String hourstr = splitestring (Time, ":", "Index", "front"); When
String minutestr = splitestring (Time, ":", "Index", "back"); Part

int currentyear = integer.valueof (Yearstr.trim ()). Intvalue ();
int currentmonth = integer.valueof (Monthstr.trim ()). Intvalue ()-1;
int currentday = integer.valueof (Daystr.trim ()). Intvalue ();
int currenthour = integer.valueof (Hourstr.trim ()). Intvalue ();
int currentminute = integer.valueof (Minutestr.trim ()). Intvalue ();

Calendar.set (CurrentYear, Currentmonth, Currentday, Currenthour,
Currentminute);
return calendar;
}

/**
* Intercept substring
*
* @param srcstr
* Source String
* @param pattern
* Match mode
* @param indexorlast
* @param frontorback
* @return
*/
public static string Splitestring (string srcstr, String pattern,
String indexorlast, String frontorback) {
String result = "";
int loc =-1;
if (indexorlast.equalsignorecase ("index")) {
LOC = Srcstr.indexof (pattern); Gets the position of the first occurrence of the string
} else {
LOC = Srcstr.lastindexof (pattern); The position of the last matching string
}
if (Frontorback.equalsignorecase ("front")) {
if (Loc!=-1)
result = srcstr.substring (0, loc); To intercept a child string
} else {
if (Loc!=-1)
result = srcstr.substring (loc + 1, srcstr.length ()); To intercept a child string
}
return result;
}

}


/datetimepicker/src/com/wwj/datetimepicker/datetimepickeractivity.java

Package com.wwj.datetimepicker;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.EditText;

/**
* Time Picker interface
*
* @author wwj_748
*
*/
public class Datetimepickeractivity extends activity {
/** called the activity is a. */
Private EditText StartDateTime;
Private EditText EndDateTime;

Private String Initstartdatetime = "September 3, 2013 14:44"; Initialization start time
Private String Initenddatetime = "August 23, 2014 17:44"; Initialization end Time

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Two input boxes
StartDateTime = (edittext) Findviewbyid (r.id.inputdate);
EndDateTime = (edittext) Findviewbyid (R.ID.INPUTDATE2);

Startdatetime.settext (Initstartdatetime);
Enddatetime.settext (Initenddatetime);

Startdatetime.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {

Datetimepickdialogutil Datetimepickdialog = new Datetimepickdialogutil (
Datetimepickeractivity.this, Initenddatetime);
Datetimepickdialog.datetimepickdialog (StartDateTime);

}
});

Enddatetime.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {
Datetimepickdialogutil Datetimepickdialog = new Datetimepickdialogutil (
Datetimepickeractivity.this, Initenddatetime);
Datetimepickdialog.datetimepickdialog (EndDateTime);
}
});
}
}

Custom interface:
/datetimepicker/res/layout/common_datetime.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<datepicker
Android:id= "@+id/datepicker"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:calendarviewshown= "false"/>

<timepicker
Android:id= "@+id/timepicker"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>

</LinearLayout>

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.