1. Declare the EDITTEXT variable and associate it with the corresponding control
Private EditText sellstarttime;
Private EditText Sellendtime;
Sellstarttime = (edittext) Findviewbyid (r.id.editsellstarttime);
Sellendtime = (edittext) Findviewbyid (r.id.editsellendtime);
2. Date selector function
Private Datepickerdialog.ondatesetlistener Mdatesetlistener = new Datepickerdialog.ondatesetlistener () {p
ublic void Ondateset (datepicker view, int, int monthofyear, int dayofmonth) {myear = year;
String mm;
String DD;
if (monthofyear<=9) {mmonth = monthofyear+1;
mm= "0" +mmonth;
} else{mmonth = monthofyear+1;
Mm=string.valueof (Mmonth);
} if (dayofmonth<=9) {mday = DayOfMonth;
dd= "0" +mday;
} else{mday = DayOfMonth;
Dd=string.valueof (Mday);
} mday = DayOfMonth;
if (Timeflag = = 0) { Sellstarttime.settext (string.valueof (myear) + "-" +mm+ "-" +dd);
}else {sellendtime.settext (string.valueof (myear) + "-" +mm+ "-" +dd);
}
}
};
protected Dialog oncreatedialog (int id) {switch (ID) {Case 0:return new Datepickerdialog (This,
Mdatesetlistener, Myear, Mmonth, Mday); Case 1:return The new Datepickerdialog (this, Mdatesetlistener, Myear, Mmonth
, Mday);
return null; }//Hide mobile keyboard private void Hideim (View EDT) {try {Inputmethodmanager im = (inputmethodman
Ager) Getsystemservice (Activity.input_method_service);
IBinder Windowtoken = Edt.getwindowtoken ();
if (Windowtoken!= null) {Im.hidesoftinputfromwindow (windowtoken, 0);
} catch (Exception e) {
}
}
3. Registers the listener, when EditText obtains the focus, or when is clicked, opens the date selector
Sellstarttime.setonclicklistener (New Onclicklistener () {public
void OnClick (View v) {
timeflag = 0;
ShowDialog (0);
}
});
Sellendtime.setonclicklistener (New Onclicklistener () {public
void OnClick (View v) {
timeflag = 1;
ShowDialog (1);
}
});
Sellstarttime.setonfocuschangelistener (New Onfocuschangelistener () {public
void Onfocuschange (View V, Boolean Hasfocus) {
if (Hasfocus = = True) {
Timeflag = 0;
Hideim (v);
ShowDialog (0);
}}}
);
Sellendtime.setonfocuschangelistener (New Onfocuschangelistener () {public
void Onfocuschange (View V, Boolean Hasfocus) {
if (Hasfocus = = True) {
timeflag = 1;
Hideim (v);
ShowDialog (1);
}}}
);
4. In the linearlayout containing edittext, add the statement
Android:focusable= "true" android:focusableintouchmode= "true"
This sentence is to prevent the edittext from automatically getting focus, triggering the date selector.