---
title: "日期编辑框"
author: "AndLi"
date: "2012-03-11T13:21:50Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2012-03-11T13:21:50Z"
id: 1450170
url: "https://andli.iteye.com/blog/1450170"
---

![](http://dl.iteye.com/upload/attachment/0064/6362/520f3480-f7d1-3cdd-85b3-51b1236b83fb.jpg)  
![](http://dl.iteye.com/upload/attachment/0064/6364/de7d913c-efeb-363b-8ac9-6a6ac1236a00.jpg)

 

 

 

```
 <EditText
        android:id="@+id/brithday_edit"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:layout_below="@id/username_edit"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@id/age_view"
        android:editable="false"
        android:gravity="center"
        android:hint="@string/selectdate"
        android:textSize="13sp" />
```

 private void getBirthday() {

```
		cal = Calendar.getInstance();
		mBirthdayText = (EditText) findViewById(R.id.brithday_edit);
		mBirthdayText.setOnClickListener(new EditText.OnClickListener() {

			@Override
			public void onClick(View v) {
				// 构建一个日期对话框
				new DatePickerDialog(MainActivity.this, selectDateListener, cal
						.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
						.get(Calendar.DAY_OF_MONTH)).show();
			}

		});
	}

	/**
	 * 日期选择对话框的DateSet事件监听
	 */
	private DatePickerDialog.OnDateSetListener selectDateListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");// 格式化日期
			mBirthdayText.setText(df.format(cal.getTime()));
		}

	};
```