Android Phone Development: Edittext+datepicker with date selector edit box

Source: Internet
Author: User

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.

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.