Android-Date and Time Control (12), android-date Control

Source: Internet
Author: User
Tags dateformat

Android-Date and Time Control (12), android-date Control

1. First, let's look at the implementation results:


Click date "". The select date dialog box is displayed.


Click time. The time dialog box is displayed.


II. The Code is as follows:

1. xml file

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "horizontal" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: id = "@ + id/txtDate" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "click to set date"/> <TextView android: id = "@ + id/txtTime" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "2dp" android: text = "click to set the time"/> </LinearLayout>

2. The background code is as follows:

Package com. example. time; import java. SQL. date; import java. text. dateFormat; import java. text. simpleDateFormat; import java. util. calendar; import java. util. locale; import android. OS. bundle; import android. app. activity; import android. app. datePickerDialog; import android. app. timePickerDialog; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. datePicker; import android. widget. textView; import android. widget. timePicker; public class MainActivity extends Activity {private final static String TAG = "TimeDate"; // obtain DateFormat fmtDate = new java. text. simpleDateFormat ("yyyy-MM-dd"); DateFormat fmtTime = new java. text. simpleDateFormat ("HH: mm: ss"); // defines a TextView control object TextView txtDate = null; TextView txtTime = null; // gets a Calendar Object Calendar dateAndTime = Calendar. getInstance (Locale. CHINA); // this method is called when you click the DatePickerDialog control setting button. onDateSetListener d = new datepickerdiener. onDateSetListener () {@ Override public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {// modify the year, month, day, and year of the calendar control, the value of monthOfYear and dayOfMonth is consistent with the latest value set by the DatePickerDialog control dateAndTime. set (Calendar. YEAR, year); dateAndTime. set (Calendar. MONTH, monthOfYear); dateAndTime. set (Calendar. DAY_OF_MONTH, dayOfMonth); // update the display of TextView to upDateDate () ;}}; TimePickerDialog. onTimeSetListener t = new timepickerdiener. onTimeSetListener () {// same as the DatePickerDialog control @ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {dateAndTime. set (Calendar. HOUR_OF_DAY, hourOfDay); dateAndTime. set (Calendar. MINUTE, minute); upDateTime () ;};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Log. d (TAG, "onCreate"); txtDate (textview1_findviewbyid(r.id.txt Date); txtDate. setClickable (true); txtDate. setFocusable (true); txtTime (textview1_findviewbyid(r.id.txt Time); txtTime. setClickable (true); txtTime. setFocusable (true); txtDate. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Log. d (TAG, "txtDate click start"); DatePickerDialog dateDlg = new DatePickerDialog (MainActivity. this, d, dateAndTime. get (Calendar. YEAR), dateAndTime. get (Calendar. MONTH), dateAndTime. get (Calendar. DAY_OF_MONTH); dateDlg. show (); Log. d (TAG, "Date show") ;}}); txtTime. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Log. d (TAG, "txtTime click start"); Log. d (TAG, "Date show"); TimePickerDialog timeDlg = new TimePickerDialog (MainActivity. this, t, dateAndTime. get (Calendar. HOUR_OF_DAY), dateAndTime. get (Calendar. MINUTE), true); timeDlg. show () ;}}); upDateDate (); upDateTime () ;}private void upDateDate () {txtDate. setText (fmtDate. format (dateAndTime. getTime ();} private void upDateTime () {txtTime. setText (fmtTime. format (dateAndTime. getTime () ;}@ Overridepublic 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 ;}}



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.