Advertisement
Guest User

DatePicker

a guest
Oct 13th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. public class DatepickerActivity extends Activity implements OnClickListener{
  2.     private TextView mDateDisplay;
  3.     private Button mPickDate;
  4.     private int mYear ;
  5.     private int mMonth ;
  6.     private int mDay;
  7.  
  8.     static final int DATE_DIALOG_ID = 1;
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.         mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
  16.         mPickDate = (Button) findViewById(R.id.pickDate);
  17.        
  18.         mPickDate.setOnClickListener(this);
  19.        
  20.         final Calendar c = Calendar.getInstance();
  21.         mYear = c.get(Calendar.YEAR);
  22.         mMonth = c.get(Calendar.MONTH);
  23.         mDay = c.get(Calendar.DAY_OF_MONTH);
  24.  
  25.         // display the current date (this method is below)
  26.         updateDisplay();
  27.  
  28.     }
  29.     private void updateDisplay() {
  30.         // TODO Auto-generated method stub
  31.          mDateDisplay.setText(
  32.                     new StringBuilder()
  33.                             // Month is 0 based so add 1
  34.                             .append(mMonth+1).append("-")
  35.                             .append(mDay).append("-")
  36.                             .append(mYear).append(" "));
  37.  
  38.        
  39.     }
  40.     @Override
  41.     public void onClick(View v) {
  42.         // TODO Auto-generated method stub
  43.         showDialog(DATE_DIALOG_ID);
  44.        
  45.     }
  46.      private DatePickerDialog.OnDateSetListener mDateSetListener =
  47.                 new DatePickerDialog.OnDateSetListener() {
  48.  
  49.                     public void onDateSet(DatePicker view, int year,
  50.                                           int monthOfYear, int dayOfMonth) {
  51.                         mYear = year;
  52.                         mMonth = monthOfYear;
  53.                         mDay = dayOfMonth;
  54.                         updateDisplay();
  55.                     }
  56.                 };
  57.                
  58.                 @Override
  59.                 protected Dialog onCreateDialog(int id) {
  60.                     switch (id) {
  61.                     case DATE_DIALOG_ID:
  62.                         return new DatePickerDialog(this,
  63.                                     mDateSetListener,
  64.                                     mYear, mMonth, mDay);
  65.                     }
  66.                     return null;
  67.                 }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement