SoFunction
Updated on 2025-03-11

The usage of datepicker control for Android

As shown below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  >
<TextView 
  android: 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  />
<Button 
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Change the date"
  />
</LinearLayout>
package .mars_2200_datepicker;

import ;

import .;
import .;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends ActionBarActivity {
  private TextView textViewDateDisplay;
  private Button btnPickDate;
  private int mYear;
  private int mMonth;
  private int mDay;

  static final int DATE_DIALOG_ID = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    textViewDateDisplay = (TextView) findViewById();
    btnPickDate = (Button) findViewById();

    (new () {
      public void onClick(View v) {
        showDialog(DATE_DIALOG_ID);
      }
    });
    final Calendar c = ();
    mYear = ();
    mMonth = ();
    mDay = (Calendar.DAY_OF_MONTH);

    updateDisplay();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in .
    int id = ();
    if (id == .action_settings) {
      return true;
    }
    return (item);
  }

  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      View rootView = (.fragment_main, container,
          false);
      return rootView;
    }
  }

  private void updateDisplay() {
    (new StringBuilder().append(mYear).append("-")
        .append(mMonth + 1).append("-").append(mDay).append(" "));
  }

  private  mDateSetListener = new () {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
      mYear = year;
      mMonth = monthOfYear;
      mDay = dayOfMonth;
      updateDisplay();
    }
  };

  protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
      return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
          mDay);
    }
    return null;
  }
}

The above is the full content of the usage of the datepicker control brought to you by the editor. I hope it will be helpful to everyone and support me more~