实施你的活动:
实现 DatePickerDialog.OnDateSetListener {
在onCreate中,当你点击按钮时,会得到日期选择器对话框。
calendar2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Initialize a new date picker dialog fragment
DialogFragment dFragment = new DatePickerFragment();
// calendar2.setText(dateDisplay);
// Show the date picker dialog fragment
dFragment.show(getFragmentManager(), "Date Picker");
外面的oncreate
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// Calendar cal = new GregorianCalendar(year, month, day);
month = month + 1;
Log.d ( TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year );
String date = day + "/" + month + "/" + year;
// mDisplayDate.setText ( date );
setDate(date);
private void setDate(String date) {
// final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
((TextView) findViewById(R.id.calendar2))
.setText(date);
public static class DatePickerFragment extends DialogFragment implements android.app.DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
Create a DatePickerDialog using Theme.
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
int year, int monthOfYear, int dayOfMonth)
// DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
// android.app.DatePickerDialog dpd = new android.app.DatePickerDialog(getActivity(),
// AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, year, month, day);
android.app.DatePickerDialog dpd = new android.app.DatePickerDialog(getActivity(),
R.style.DatePickerDialogTheme, (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
// new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,
// (DatePickerDialog.OnDateSetListener)
// getActivity(), year, month, day)
// DatePickerDialog THEME_DEVICE_DEFAULT_DARK
android.app.DatePickerDialog dpd2 = new android.app.DatePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_DARK, this, year, month, day);
// DatePickerDialog THEME_HOLO_LIGHT
android.app.DatePickerDialog dpd3 = new android.app.DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
dpd.getDatePicker().getTouchables().get(0).performClick();
// DatePickerDialog THEME_HOLO_DARK
android.app.DatePickerDialog dpd4 = new android.app.DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_DARK, this, year, month, day);
// DatePickerDialog THEME_TRADITIONAL
android.app.DatePickerDialog dpd5 = new android.app.DatePickerDialog(getActivity(),
AlertDialog.THEME_TRADITIONAL, this, year, month, day);
// TextView tv = new TextView(getActivity());
// // Create a TextView programmatically
// ViewGroup.LayoutParams lp = new RelativeLayout.LayoutParams(
// ViewGroup.LayoutParams.WRAP_CONTENT, // Width of TextView
// ViewGroup.LayoutParams.WRAP_CONTENT); // Height of TextView
// tv.setLayoutParams(lp);
// tv.setPadding(10, 10, 10, 10);
// tv.setGravity(Gravity.CENTER);
// tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
// tv.setText(getResources().getText(R.string.Date_Of_Birth));
// tv.setTextColor(Color.parseColor("#ff0000"));
// tv.setBackgroundColor(Color.parseColor("#FFD2DAA7"));
// Set the newly created TextView as a custom tile of DatePickerDialog
//dpd.setCustomTitle(tv);
// Or you can simply set a tile for DatePickerDialog
setTitle(CharSequence title)
Set the title text for this dialog's window.
dpd3.setTitle(getResources().getText(R.string.Date_Of_Birth));
// Return the DatePickerDialog
return dpd;
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
month = month + 1;
Log.d ( TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year );
// dateDisplay = day + "/" + month + "/" + year;
// mDisplayDate.setText ( date );
In styles.xml