Call of the time dialog box in Android development, Android dialog box
In Android development, the time dialog box is often used. Generally, the date is selected in an EditText file. Next we will learn how to use the time dialog box.
1. First, set two controls in the corresponding XML file, one TextView and one EditText. Write operations on EditText in activity.
2. Declare the variable (year, month, day) and txtTime (EditText control, used to display the time) and initialize it accordingly.
TextView tvInTime; EditText txtInTime; // year, month, and day private int mYear; private int mMonth; private int mDay;
tvInTime =(TextView) findViewById(R.id.tvInTime);txtInTime = (EditText) findViewById(R.id.txtInTime);
3. Set a click RESPONSE event for the time text box
// Set and click txtInTime for the listening event in the time text box. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// display date selection dialog box showDialog (DATE_DIALOG_ID );}});
4. initialize the Calendar Object and display the current system time.
// Initialize the Calendar Object final Calendar c = Calendar. getInstance (); mYear = c. get (Calendar. YEAR); // obtain the YEAR mMonth = c. get (Calendar. MONTH); // obtain the MONTH mDay = c. get (Calendar. DAY_OF_MONTH); // obtain the number of days // display the current system time updateDisplay ();
5. Customize the display time of the method.
// Use the OnDateSetListener listener to set the system time dialog box private DatePickerDialog. onDateSetListener mDateSetListener = new DatePickerDialog. onDateSetListener () {public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {mYear = year; // assign mMonth = monthOfYear to the year; // assign mDay = dayOfMonth to the month; // assign updateDisplay () to the day; // display the set date }}; /** custom method to display the system time */private void updateDisplay () {// display the set time txtInTime. setText (new StringBuilder (). append (mYear ). append ("-"). append (mMonth + 1 ). append ("-"). append (mDay ));}
Of course, some other controls are required in the XML file, such as buttons. You can set them and set the corresponding methods. Here is a brief introduction to the pop-up of the time dialog box and the methods you can choose freely.