Datepicterdialog and Timepicterdialog Cancel button

Source: Internet
Author: User

This is the first pop-up of the date popup: separate pop-up datepicterdialog and Timepicterdialog

Final Datepickerdialog Datepickerdialog = new Datepickerdialog (
Tablechart.this, NULL, Myear, Mmonth,
Mday);
Datepickerdialog.setbutton (Dialoginterface.button_positive,
"Done", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface arg0,
int arg1) {
Datetimeutils.showdatepositive (Datepickerdialog,starttime);
}
});
Datepickerdialog.setbutton (Dialoginterface.button_negative,
"Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface arg0,
int arg1) {
Datetimeutils.showdatenegative (starttime);
}
});
Datepickerdialog.setcanceledontouchoutside (FALSE);
Datepickerdialog.show ();

public static void Showdatepositive (Datepickerdialog datepickerdialog,
EditText Yearedit) {
DatePicker DatePicker = Datepickerdialog.getdatepicker ();//Get Datepicter Object
int year = Datepicker.getyear ();
int month = Datepicker.getmonth () + 1;
int day = Datepicker.getdayofmonth ();
Yearedit.settext (year+ "-" month+ "-" +day ");
Yearedit.clearfocus ();
}
public static void Showdatenegative (EditText yearedit) {
Yearedit.clearfocus ();
}
Because Timepicterdialog cannot get to Timepicter, rewrite: Mytimepickerdialog.java
Package com.rts.dzdc.utils;
Import COM.RTS.DZDC.ACTIVITY.R;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.TimePicker;
Import Android.widget.TimePicker.OnTimeChangedListener;
/**
* Basically apply the source code for a bit
* @author Lee
*
*/
public class Mytimepickerdialog extends Alertdialog
Implements Onclicklistener, Ontimechangedlistener {
Public interface Ontimesetlistener {
void Ontimeset (timepicker view, int hourofday, int minute);
}
private static final String HOUR = "HOUR";
private static final String MINUTE = "MINUTE";
private static final String Is_24_hour = "Is24hour";
Private final Timepicker Mtimepicker;
Private final Ontimesetlistener Mcallback;
int minitialhourofday;
int Minitialminute;
Boolean Mis24hourview;

Public Mytimepickerdialog (Context context,
Ontimesetlistener CallBack,
int hourofday, int minute, Boolean Is24hourview) {
This (context, 0, CallBack, hourofday, minute, Is24hourview);
}

Public Mytimepickerdialog (Context context,
int theme,
Ontimesetlistener CallBack,
int hourofday, int minute, Boolean Is24hourview) {
Super (context, theme);
Mcallback = CallBack;
Minitialhourofday = Hourofday;
Minitialminute = minute;
Mis24hourview = Is24hourview;
SetIcon (0);
Settitle ("Set Time");
Context Themecontext = GetContext ();
Setbutton (button_positive, "done", this);
Layoutinflater Inflater =
(Layoutinflater) Themecontext.getsystemservice (Context.layout_inflater_service);
View view = Inflater.inflate (R.layout.time_picker_dialog, NULL);
Setview (view);
Mtimepicker = (timepicker) View.findviewbyid (R.id.timepicker);
Initialize State
Mtimepicker.setis24hourview (Mis24hourview);
Mtimepicker.setcurrenthour (Minitialhourofday);
Mtimepicker.setcurrentminute (Minitialminute);
Mtimepicker.setontimechangedlistener (this);
}
public void OnClick (Dialoginterface dialog, int which) {
Trynotifytimeset ();
}
public void UpdateTime (int hourofday, int minutofhour) {
Mtimepicker.setcurrenthour (Hourofday);
Mtimepicker.setcurrentminute (Minutofhour);
}
public void ontimechanged (timepicker view, int hourofday, int minute) {
/* do nothing */
}
private void Trynotifytimeset () {
if (mcallback! = null) {
Mtimepicker.clearfocus ();
Mcallback.ontimeset (Mtimepicker, Mtimepicker.getcurrenthour (),
Mtimepicker.getcurrentminute ());
}
}
@Override
protected void OnStop () {
Trynotifytimeset ();
Super.onstop ();
}
@Override
Public Bundle onsaveinstancestate () {
Bundle state = Super.onsaveinstancestate ();
State.putint (HOUR, Mtimepicker.getcurrenthour ());
State.putint (MINUTE, Mtimepicker.getcurrentminute ());
State.putboolean (Is_24_hour, Mtimepicker.is24hourview ());
return state;
}
@Override
public void Onrestoreinstancestate (Bundle savedinstancestate) {
Super.onrestoreinstancestate (savedinstancestate);
int hour = Savedinstancestate.getint (hour);
int minute = Savedinstancestate.getint (minute);
Mtimepicker.setis24hourview (Savedinstancestate.getboolean (Is_24_hour));
Mtimepicker.setcurrenthour (hour);
Mtimepicker.setcurrentminute (minute);
}

/**
* Add yourself to get Timepicker method
* @return
*/
Public Timepicker Gettimepicker ()
{
return mtimepicker;
}
}
How to use:
Final Mytimepickerdialog Timepickerdialog = new Mytimepickerdialog (
Tableslidezaihaiactivity.this, NULL,
Mhour, Mmintue, true);
Timepickerdialog.setbutton (Dialoginterface.button_positive,
"Done", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface arg0,
int arg1) {
Datetimeutils.showtimepositive (Timepickerdialog,hour);
}
});
Timepickerdialog.setbutton (Dialoginterface.button_negative,
"Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface arg0,
int arg1) {
Datetimeutils.showtimenegative (Slidedown_huadongtimehour);
}
});
Timepickerdialog.setcanceledontouchoutside (FALSE);
Timepickerdialog.show ();

public static void Showtimepositive (Mytimepickerdialog timepickerdialog,edittext houredit) {
Timepicker Timepicker = Timepickerdialog.gettimepicker ();
int hour = Timepicker.getcurrenthour ();
int mintue = Timepicker.getcurrentminute ();
Houredit.settext (hour+ ":" +mintue);
Houredit.clearfocus ();
}
public static void Showtimenegative (EditText houredit, EditText) {
Houredit.clearfocus ();
}

This article is from the "It Software Development Materials" blog, please be sure to keep this source http://491733638.blog.51cto.com/3913830/1690540

Datepicterdialog and Timepicterdialog Cancel button

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.