效果图:
代码:
——XML——
<EditText
android:id="@+id/sssx_bbdy_cx_sbrq"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/sssx_bbdy_cx_btn"
android:background="@drawable/ed_biankuang_yuanjiao"
android:digits="0123456789"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="点击选择日期"
android:paddingLeft="15dp"
android:textColor="#3f3f3f"
android:textSize="15sp" />
——java——
dy_sbrq = (EditText) findViewById(R.id.sssx_bbdy_cx_sbrq);
dy_sbrq.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(0);
}
});
protected Dialog onCreateDialog(int id) {
Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);
mYear = dateAndTime.get(Calendar.YEAR);
mMonth = dateAndTime.get(Calendar.MONTH);
mDay = dateAndTime.get(Calendar.DAY_OF_MONTH);
switch (id) {
case 0:
return new MonPickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
String mm;
String dd;
if (monthOfYear <= 9) {
mMonth = monthOfYear + 1;
mm = "0" + mMonth;
} else {
mMonth = monthOfYear + 1;
mm = String.valueOf(mMonth);
}
if (dayOfMonth <= 9) {
mDay = dayOfMonth;
dd = "0" + mDay;
} else {
mDay = dayOfMonth;
dd = String.valueOf(mDay);
}
mDay = dayOfMonth;
dy_sbrq.setText(String.valueOf(mYear) + mm);
}
};
———MonPickerDialog.java———
public class MonPickerDialog extends DatePickerDialog {
@SuppressLint("NewApi")
public MonPickerDialog(Context context, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
this.setTitle(year + "年" + (monthOfYear + 1) + "月");
((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0))
.getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
this.setTitle(year + "年" + (month + 1) + "月");
}
}