To pop up the date setting as a dialog box:
first create a edittext input box in the XML:
<edittext
Android:id= "@+id/et_time"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
//Set Input box click event
android:onclick= "Dateshow"/>
Setting the modification time in the oncreate of the Main method
Private EditText time;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Time = (EditText) Findviewbyid (r.id.et_time);
Calendar calendar = Calendar.getinstance ();
//Get date
NewYear = Calendar.get (calendar.year);
//Because the month is from 0--11 so add 1
Newmonth = Calendar.get (calendar.month) + 1;
NewDay = Calendar.get (calendar.day_of_month);
Update ();
}
public void Dateshow (View v) {
//Click EditText to pop up the modified date in the form of a dialog box
Oncreatdialog (date_show). SHOW ();
}
protected Dialog oncreatdialog (int id) {
Switch (ID) {
Case Date_show:
return new Datepickerdialog (Incomeactivity.this, Datesetlistener,
NewYear, Newmonth, NewDay);
}
return null;
}
Private Datepickerdialog.ondatesetlistener Datesetlistener = new Ondatesetlistener () {
@Override
public void Ondateset (DatePicker view, int year, int monthofyear,
int DayOfMonth) {
TODO auto-generated Method Stub
//Modified year
NewYear = year;
//Modified month
Newmonth = Monthofyear;
//Date Modified
NewDay = DayOfMonth;
Update ();
}
};
private void Update () {
TODO auto-generated Method Stub
//For concatenation of multiple strings
StringBuffer sb = new StringBuffer ();
//Set the modified date time to EditText
Time.settext (Sb.append (NewYear). Append ("-"). Append (Newmonth)
. Append ("-"). Append (NewDay));
}
This article is from the "Android Small Notes" blog, please be sure to keep this source http://dreamwing.blog.51cto.com/9872128/1611691
Android Small Note dialog box format modified Date