The TimePickerDialog dialog box was used for the reminder function. I checked a lot of technical materials, but I felt that many things were not very detailed, and many places, there are some imperfections. For example, the pop-up dialog box does not get the current time of the system, but a phenomenon of other times, which is quite boring. After several studies, I decided to take some of my personal understanding of this space and discuss it with you. If there is anything wrong, please forgive me and we will improve it.
If you don't talk much about it, go directly to the topic:
First, declare a Calendar Object in the program and instantiate it to obtain the calendar instance, which will be used later.
Private Calendar c = Calendar. getInstance ();
After instantiation, you can operate on the calendar object, such as c. the get method can obtain the relevant variables (such as year, month, day, hour, minute, and second) in the Calendar Object. The value of these variables is instantiated as "c = Calendar. getInstance (); "has been set as the system default time; use c. the set method can be used to set the relevant variables of the Calendar Object.
Another important method for calendar objects is setTimeInMillis, which has only one parameter, namely the number of milliseconds between 0 hours on January 1, January 1, 1970. Call this method, the variables in the Calendar Object are set based on the number of milliseconds you have passed in. To set the variables to the current system time, use the following method: "c. setTimeInMillis (System. currentTimeMillis ());"
The main character is the development of the time dialog box. Provide some implementation code, detailed details, and research on your own
E1 = (EditText) findViewById (R. id. c1_time); e1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {c. setTimeInMillis (System. currentTimeMillis (); int mHour = c. get (Calendar. HOUR_OF_DAY); int mMinute = c. get (Calendar. MINUTE); new TimePickerDialog (ClassTimeSet. this, new TimePickerDialog. onTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {c. setTimeInMillis (System. currentTimeMillis (); c. set (Calendar. HOUR_OF_DAY, hourOfDay); c. set (Calendar. MINUTE, minute); c. set (Calendar. SECOND, 0); // set to 0 c. set (Calendar. MILLISECOND, 0); // set to 0 }}, mHour, mMinute, true ). show ();}});
// Determine whether the time is less than 10 hours and minutes. String strHourOfDay = hourOfDay <10? "0" + hourOfDay: "" + hourOfDay;
String strMinute = minute <10? "0" + minute: "" + minute;
The TimePickerDialog method has five parameters, the first parameter (MenuView. this) is the activity pointer of the pop-up time dialog box. The second parameter is the last parameter. The third parameter (hour) and the fourth parameter (minute) it is the initial hours and minutes displayed in the pop-up time dialog box. These two variables are initialized in the blue code. The fifth parameter is the 24-hour display parameter, true indicates that the time is displayed in the 24-hour format.