首页 > 代码库 > 重写DatePicker------只显示 年-月

重写DatePicker------只显示 年-月


代码不多,话不多说




/**
 * 重写datePicker 1.只显示 年-月 2.title 只显示 年-月
 * @author lmw
 */
public class MonPickerDialog extends DatePickerDialog {
	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) + "月");
	}

}


调用和赋值:

代码里的mon_date_filter 是显示 年-月的控件(button或者textView)

	public void showMonPicker() {
		final Calendar localCalendar = Calendar.getInstance();
		localCalendar.setTime(DateUtils.strToDate("yyyy-MM", mon_date_filter.getText().toString()));
		new MonPickerDialog(this,new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
						localCalendar.set(1, year);
						localCalendar.set(2, monthOfYear);
						mon_date_filter.setText(DateUtils.clanderTodatetime(localCalendar, "yyyy-MM"));
					}
				}, localCalendar.get(1), localCalendar.get(2),localCalendar.get(5)).show();
	}