Back to project page android_tutorial_projects.
The source code is released under:
Copyright (c) 2013, Uthcode All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redi...
If you think the Android project android_tutorial_projects listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uthcode.exampledatepicker; /*from www. j a v a 2 s . c om*/ import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import java.util.Calendar; public class MainActivity extends Activity { static final int DATE_DIALOG_ID = 999; private int year; private int month; private int day; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setCurrentDateOnView(); addListenerOnButton(); } public void setCurrentDateOnView() { TextView tvDisplayDate = (TextView) findViewById(R.id.tvDate); DatePicker dpResult = (DatePicker) findViewById(R.id.dpResult); final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); // set current date into textview tvDisplayDate.setText(new StringBuilder() .append(month + 1).append("-").append(day).append("-") .append(year).append(" ")); // set current date into datePicker dpResult.init(year, month, day, null); } public void addListenerOnButton() { Button btnChangeDate = (Button) findViewById(R.id.btnChangeDate); btnChangeDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDialog(DATE_DIALOG_ID); } }); } /* @Override protected Dialog onCreateDialog(int id) { switch(id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, datePickerListener, year, month, day); } return null; } private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; tvDate.setText(new StringBuilder().append(month+1) .append("-") .append(day) .append("-") .append(year) .append(" ")); dpResult.init(year, month, day, null); } }; */ }