android日历事件 简书,android 只显示年月的日历

这是一篇不完整的文章 希望看到的读者 能够 跟我完善一下 !

这系统自带的日历 多多少少 会有弊端 !

手机版本不一样 操作不一样 所以我们在判断隐藏某一部分之前 还要判断api的版本

cbee3f0c7a2f?d=123

这是我要达成的效果图 .png

1.首先继承一下系统自带的日历dialog

public class YearPickerDialog extends DatePickerDialog {

public YearPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {

super(context, callBack, year, monthOfYear, dayOfMonth);

}

@Override

public void onDateChanged(DatePicker view, int year, int month, int day) {

super.onDateChanged(view, year, month, day);

this.setTitle(year + "年" );

}

}

2.写一个遍历方法

private DatePicker findDatePicker(ViewGroup group) {

if (group != null) {

for (int i = 0, j = group.getChildCount(); i < j; i++) {

View child = group.getChildAt(i);

if (child instanceof DatePicker) {

return (DatePicker) child;

} else if (child instanceof ViewGroup) {

DatePicker result = findDatePicker((ViewGroup) child);

if (result != null)

return result;

}

}

}

return null;

}

3.调用

final Calendar calendar = Calendar.getInstance();

YearPickerDialog dialog= new YearPickerDialog(getActivity(), 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);

LogUtils.d(DateUtil.clanderTodatetime(calendar, "yyyy-MM"));

}

},calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));

dialog.show();

DatePicker dp = findDatePicker((ViewGroup) dialog.getWindow().getDecorView());

if (dp != null) {

((ViewGroup)((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE);

}

4.其中的DateUtil 类方法

public static String clanderTodatetime(Calendar calendar, String style) {

SimpleDateFormat formatter = new SimpleDateFormat(style);

return formatter.format(calendar.getTime());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值