Android日历只显示年月,只显示年
16lz
2021-01-23
Android日历只显示年月
款式如下:
public class YearPickerDialog extends DatePickerDialog { public YearPickerDialog(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(1).setVisibility(View.GONE); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); } public YearPickerDialog(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { super(context, theme, listener, year, monthOfYear, dayOfMonth);// this.setTitle(year + "年" + (monthOfYear + 1) + "月"); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE); ((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 + "年" ); }}
Android日历只显示年
款式如下:
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 + "年"); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); } public MonPickerDialog(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { super(context, theme, listener, year, monthOfYear, dayOfMonth);// this.setTitle(year + "年" ); ((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 + "年"); }}
关于使用
private void selectMonthTime() { new MonPickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, monthOfYear); id_tv_time_month.setText(DateUtil.clanderTodatetime(calendar, "yyyy-MM")); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show(); }
DateUtils
public class DateUtil { public static Date strToDate(String style, String date) { SimpleDateFormat formatter = new SimpleDateFormat(style); try { return formatter.parse(date); } catch (ParseException e) { e.printStackTrace(); return new Date(); } } public static String dateToStr(String style, Date date) { SimpleDateFormat formatter = new SimpleDateFormat(style); return formatter.format(date); } public static String clanderTodatetime(Calendar calendar, String style) { SimpleDateFormat formatter = new SimpleDateFormat(style); return formatter.format(calendar.getTime()); } public static String DateTotime(long date, String style) { SimpleDateFormat formatter = new SimpleDateFormat(style); return formatter.format(date); }
更多相关文章
- Android中的日历读写操作!!!
- 日历视图的XML属性
- android日历实现__GO桌面版&&开源收藏版
- android的map上的浮动菜单以及日历控件
- android日历实现
- android 中的日历控件
- Android实现日历
- 日历
- 详解关于Blog实现一个日历的实例教程