Android notes -- date & amp; time (date and time dialog box)

Source: Internet
Author: User

Android notes -- date & amp; time (date and time Selection dialog box)
TimePickerDialog (Time Selection dialog box)

Create TimePickerDialog time dialog box:

1. Create a class to inherit DialogFragement

2. Override the onCreateDialog () method and return a TimePickerDialog object.

3. Implement the OnTimeSetListener interface of TimePickerDialog to receive a callback.

DatePickerDialog (date selection dialog box)

Create DatePickerDialog date dialog box:

1. Create a class to inherit DialogFragment

2. Override the onCreateDialog () method and return a TimePickerDialog object.

3. Implement the OnDateSetListener interface of DatePickerDialog to receive a callback.


DatePickerDialog and TimePickerDialog are provided by Android to bring up a date and time selection dialog box. We can get a date and time dialog box by instantiating DatePickerDialog and TimePickerDialog in the program. Both classes are subclasses of AlertDialog.

I provide a Baidu official description of Picker, may be helpful for understanding this article: http://wenku.baidu.com/link? Url = U77TPsl99lpf8QrGD2h6XKA6xJqzk-JseCnJhC9VmgYoWckhCZ8-3F3TzOkOZGLverjwt6JitlhW-wWMODAOQLtEpVxryaZxNlCQstOjLCK

Click Open Link

Source code:

Html

     
      
      
      
      
 


Java

Import java. util. calendar; import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. app. datePickerDialog; import android. app. dialog; import android. app. dialogFragment; import android. app. timePickerDialog; import android. text. format. dateFormat; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. datePicker; import android. widget. textView; import android. widget. timePicker; @ SuppressLint ({"NewApi", "ValidFragment"}) public class MainActivity extends Activity {private Button btnSetTime; private TextView TV _setTime; private TextView TV _setDate; protected void onCreate) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // instantiate the object, bind btnSetTime = (Button) findViewById (R. id. button#settime); TV _setTime = (TextView) findViewById (R. id. TV _settime); TV _setDate = (TextView) findViewById (R. id. TV _setdate);} // set the date event handling method public void setDateClick (View v) {SetDateDialog sdt = new SetDateDialog (); sdt. show (getFragmentManager (), "datePicker");} // set the time event handling method public void setTimeClick (View v) {SetTimeDialog std = new settimediick (); std. show (getFragmentManager (), "timePicker");} public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} // class SetDateDialog extends DialogFragment implements DatePickerDialog in the create date dialog box. onDateSetListener {public Dialog onCreateDialog (Bundle savedInstanceState) {// Calendar is an abstract class that obtains instances through getInstance () and sets it to the system default time final Calendar c = Calendar. getInstance (); // get year, month, day int year = c. get (Calendar. YEAR); int month = c. get (Calendar. MONTH); int day = c. get (Calendar. DAY_OF_MONTH); DatePickerDialog dpd = new DatePickerDialog (getActivity (), this, year, month, day); return dpd;} public void onDateSet (DatePicker view, int year, int month, int day) {TV _setDate.setText (year + "year" + (month + 1) + "month" + day + "day ");}} // create time dialog box @ SuppressLint ("NewApi") class SetTimeDialog extends DialogFragment implements TimePickerDialog. onTimeSetListener {@ SuppressLint ("NewApi") public Dialog onCreateDialog (Bundle savedInstanceState) {// get the hour, minutes final Calendar c = Calendar. getInstance (); int hour = c. get (Calendar. HOUR_OF_DAY); int minute = c. get (Calendar. MINUTE); TimePickerDialog tpd = new TimePickerDialog (getActivity (), this, hour, minute, DateFormat. is24HourFormat (getActivity (); return tpd;} public void onTimeSet (TimePicker view, int hourOfDay, int minute) {TV _setTime.setText (hourOfDay + ":" + minute );}}}

Effect display:



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.