/**
日期选择
-
@author 某人
*/
public class ExpirationDialog extends DialogFragment {
private String pickerTime;ExpirationDialog expirationDialog;
@BindView(R.id.picker_date)
DatePicker datePicker;
@BindView(R.id.pickerCancel)
TextView pickerCancel;
@BindView(R.id.pickerCommite)
TextView pickerCommite;Unbinder unbinder;
private int mYear, mMonth, mDay;public static ExpirationDialog newInstance() {
return new ExpirationDialog();
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}private void init() {
datePicker.init(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// 获取一个日历对象,并初始化为当前选中的时间
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM");
pickerTime = format.format(calendar.getTime());} }); hideDay(datePicker);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_expiration_dialog, container, false);
AnimationUtils.slideToUp(view);
unbinder = ButterKnife.bind(this, view);
init();
return view;
}
private void hideDay(DatePicker mDatePicker) {
try {
/* 处理android5.0以上的特殊情况 */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//根据FindView方式去获取DayView
int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
if (daySpinnerId != 0) {
View daySpinner = mDatePicker.findViewById(daySpinnerId);
if (daySpinner != null) {
daySpinner.setVisibility(View.GONE);
}
}
} else {
//根据反射的方法去获取DayView字段
Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
for (Field datePickerField : datePickerfFields) {
if ("mDaySpinner".equals(datePickerField.getName()) || ("mDayPicker").equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
try {
dayPicker = datePickerField.get(mDatePicker);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
((View) dayPicker).setVisibility(View.GONE);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@OnClick({R.id.pickerCancel, R.id.pickerCommite})
public void onClick(View view) {
switch (view.getId()) {
case R.id.pickerCancel:
break;
case R.id.pickerCommite:
if (!TextUtils.isEmpty(pickerTime)) {
if (dialogResult != null) {
dialogResult.finish(pickerTime);
}
} else {
MyToast.show(getContext(), "未选择日期!");
}
break;
default:
}
dismiss();
}
public interface ExpirationDialogInterface {
void finish(String result);
}
private ExpirationDialogInterface dialogResult;
public void setDialogResult(ExpirationDialogInterface dialogResult) {
this.dialogResult = dialogResult;
}
}