Date DatePicker and Time Timepicker control

Source: Internet
Author: User

In the Androidapp application, set the date and time is also often met, below we study together.

We need to learn the basic controls in Android: (1) Date selection control DatePicker (2) Time selection control Timepicker.


First, the Design login window

Open the "res/layout/activity_main.xml" file.

1. Drag 1 Date selection controls from the toolbar to activity DatePicker, a time selection control Timepicker, and a button. Control from Time&date, Form Widgets.

650) this.width=650; "src=" http://img.blog.csdn.net/20151023160726762 "alt=" 20151023160726762 "/>



2. Open the Activity_main.xml file.

We have modified the auto-generated code to the following code, specifically:

(1) The ID of DatePicker is modified to Tody.

(2) The ID of Timepicker is changed to now;

(3) The button's ID is modified to save and its text is modified to "save".

Second, click event

Open the "Src/com.genwoxue.datepickertimepicker/mainactivity.java" file.

Then enter the following code:

650) this.width=650; "src=" http://img.blog.csdn.net/20151023160936903 "alt=" 20151023160936903 "/>



We'll focus on the light blue background.

1. Section ①

Imports 2 packages related to DatePicker and Timepicker.

2. Section ②

Declares a 3 control variable.

3. Section ③

(1) The Findviewbyid () method completes the capture of 3 controls.

(2) "Save" button to add click Listen Event: Btnsave.setonclicklistener (New Saveonclicklistener ()).

4. Section ④

The GetYear (), GetMonth (), GetDayOfMonth () methods get the year, month, and day.

5. Section ⑤

Getcurrenthour (), Getcurrentminute () method gets the time, minute.

6. Section ⑥

Use Toast to display date DatePicker, Time Timepicker control selected date and time.

The effect is as follows:

650) this.width=650; "src=" http://img.blog.csdn.net/20151023161024822 "alt=" 20151023161024822 "/>


Third, the attached code

1, Activity_main.xml Source

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<timepicker
Android:id= "@+id/now"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/today"
Android:layout_alignparentleft= "true"/>


<button
Android:id= "@+id/save"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/now"
android:text= "@string/save"/>


<datepicker
Android:id= "@+id/today"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"/>



</RelativeLayout>



2, Mainactivity.java source

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.DatePicker;
Import Android.widget.TimePicker;
Import Android.widget.Toast;


public class Mainactivity extends Activity {
Private DatePicker dptoday = null;
Private Timepicker tpnow = null;
Private Button btnsave = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Dptoday = (DatePicker) Super.findviewbyid (r.id.today);
Tpnow = (timepicker) Super.findviewbyid (R.id.now);
Btnsave = (Button) Super.findviewbyid (R.id.save);
Btnsave.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
int iyear = 0;
int imonth = 0;
int iday = 0;
String sdate = "";
Iyear = Dptoday.getyear ();
Imonth = Dptoday.getmonth () +1;//Remember to add 1

Iday = Dptoday.getdayofmonth ();
Sdate = "Date:" +string.valueof (iyear) + "year" +string.valueof (imonth) + "Month" +string.valueof (iday) + "Day";
int ihour = 0;
int imin = 0;
String stime = "";
Ihour = Tpnow.getcurrenthour ();
Imin = Tpnow.getcurrentminute ();
Stime = "Time:" +string.valueof (Ihour) + "when" +string.valueof (imin) + "min";
Toast.maketext (Getapplicationcontext (), Sdate+stime, Toast.length_long). Show ();
}
});
}


@Override
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;
}
}



This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1705569

Date DatePicker and Time Timepicker control

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.