Date dialog box (DatePickerDialog) and time dialog box (TimePickerDialog ),

Source: Internet
Author: User

Date dialog box (DatePickerDialog) and time dialog box (TimePickerDialog ),

  • Layout
<Button android: id = "@ + id/btn_date" android: text = "pop-up date selection dialog box" android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/btn_time" android: text = "pop-up time selection dialog box" android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content"/>

 

 
  • Code
Package com. chase. datetimedialog; import android. app. datePickerDialog; import android. app. datePickerDialog. onDateSetListener; import android. app. timePickerDialog; import android. app. timePickerDialog. onTimeSetListener; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. button; import android. widget. datePicker; import android. widget. timePicker; import android. widget. toast; import java. util. calendar; public class MainActivity extends AppCompatActivity {private Button mBtnDate; private Button mBtnTime; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mBtnDate = (Button) findViewById (R. id. btn_date); mBtnTime = (Button) findViewById (R. id. btn_time); initDialog ();} private void initDialog () {/*** pop-up date selection dialog box */mBtnDate. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Calendar Ar c = Calendar ar. getInstance (); int year = c. get (Calendar. YEAR); int month = c. get (Calendar. MONTH); final int day = c. get (Calendar. DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog (MainActivity. this, new OnDateSetListener () {@ Override public void onDateSet (DatePicker view, int year, int month, int dayOfMonth) {Toast. makeText (MainActivity. this, year + "year" + (month + 1) + "month" + dayOfMonth + "day", Toast. LENGTH_SHORT ). show () ;}}, year, month, day); datePickerDialog. show () ;}});/*** Time Selection dialog box */mBtnTime. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// obtain the current system time Calendar c = Calendar. getInstance (); int hour = c. get (Calendar. HOUR_OF_DAY); int minute = c. get (Calendar. MINUTE); // The TimePickerDialog timePickerDialog = new TimePickerDialog (MainActivity. this, new OnTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {Toast. makeText (MainActivity. this, "Time" + hourOfDay + ":" + minute, Toast. LENGTH_SHORT ). show () ;}, hour, minute, true); timePickerDialog. show ();}});}}

 



From Weizhi note (Wiz)



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.