首页 > 代码库 > 重写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(); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。