xuxiuxi
2017-03-31 370f1eb822076137e3bae53e2e36451c977c8834
1
2
3
4
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cn.com.basic.face.dialog;
 
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
 
import com.bsk.zhangbo.demoforbsk.R;
 
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
/**
 * Created by Sinoe on 2017/2/28.
 */
 
public class BirthdayPickerDialog  extends AlertDialog implements
        DatePicker.OnDateChangedListener{
 
 
    private static final String YEAR = "year";
    private static final String MONTH = "month";
    private static final String DAY = "day";
 
    private final DatePicker mDatePicker;
    private final OnDateSetListener mDateSetListener;
    private final Calendar mCalendar;
 
    private boolean mTitleNeedsUpdate = true;
 
    private View view;
    public BirthdayPickerDialog(Context context, int theme, OnDateSetListener listener, Date date) {
        super(context,theme);
        mDateSetListener = listener;
        mCalendar = Calendar.getInstance(Locale.CHINA);
        mCalendar.setTime(date);
        Context themeContext = getContext();
        LayoutInflater inflater = LayoutInflater.from(themeContext);
        view = inflater.inflate(R.layout.dialog_date_picker, null);
        view.setBackgroundColor(Color.WHITE);
        //setView(view);
 
 
//        setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
//        setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
//        setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
 
        mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
        mDatePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
        //  mDatePicker.setValidationCallback(mValidationCallback);
        //实现自己的标题和ok按钮
        //setTitle("选择日期:");
        setButton();
    }
 
    @Override
    public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
        mDatePicker.init(year, month, day, this);
    }
 
 
    /**
     * The callback used to indicate the user is done filling in the date.
     */
    public interface OnDateSetListener {
 
        /**
         * @param view The view associated with this listener.
         * @param year The year that was set.
         * @param monthOfYear The month that was set (0-11) for compatibility
         *  with {@link java.util.Calendar}.
         * @param dayOfMonth The day of the month that was set.
         */
        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
    }
 
    private void setButton() {
        //获取自己定义的响应按钮并设置监听,直接调用构造时传进来的CallBack接口(为了省劲,没有自己写接口,直接用之前本类定义好的)同时关闭对话框。
        view.findViewById(R.id.date_picker_ok).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mDateSetListener != null) {
                            // Clearing focus forces the dialog to commit any pending
                            // changes, e.g. typed text in a NumberPicker.
                            mDatePicker.clearFocus();
                            mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
                                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
                            cancel();
                        }
                    }
                });
        view.findViewById(R.id.date_picker_cancle).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancel();
                    }
                });
    }
 
    public void myShow() {
        //自己实现show方法,主要是为了把setContentView方法放到show方法后面,否则会报错。
        show();
        setContentView(view);
    }
    @NonNull
    @Override
    public Bundle onSaveInstanceState() {
        return super.onSaveInstanceState();
    }
 
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
}