Android日曆只顯示年月,只顯示年
阿新 • • 發佈:2019-02-11
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);
}