DatePickerDialogFragment.java 2.13 KB
Newer Older
ydl committed
1 2 3
package com.yidianling.user.mine;

import android.os.Bundle;
YKai committed
4
import androidx.annotation.Nullable;
ydl committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;

import com.ydl.ydlcommon.base.BaseDialogFragment;
import com.yidianling.user.R;

import java.util.Calendar;


/**
 * 日期选择器 dialog
 * Created by Dog on 2015/6/12.
 */
public class DatePickerDialogFragment extends BaseDialogFragment implements DatePicker.OnDateChangedListener {

    DatePicker dp_birthday;

    Calendar calendar = Calendar.getInstance();
    DatePicker.OnDateChangedListener dateChangedListener;
    int year, month, day;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.user_mine_fragment_dialog_date_picker,container,false);
        dp_birthday = view.findViewById(R.id.dp_birthday);
        view.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
            dismiss();
        });
        view.findViewById(R.id.btn_sure).setOnClickListener(v -> {
            if (this.dateChangedListener != null) {
                dateChangedListener.onDateChanged(dp_birthday, dp_birthday.getYear(), dp_birthday.getMonth(), dp_birthday.getDayOfMonth());
            }
            dismiss();
        });
        init();
        return view;
    }

    public void setCalendar(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        calendar.set(year, month, day);
    }

    public void setDateChangedListener(DatePicker.OnDateChangedListener listener) {
        this.dateChangedListener = listener;
    }

    void init() {
        dp_birthday.init(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                this
        );


    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        this.year = year;
        this.month = monthOfYear;
        this.day = dayOfMonth;
    }


}