自定义DatePickerDialog

public class MyDatePickerDialog 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 CustomDatePicker mDatePicker;
    private final OnDateSetListener mDateSetListener;
    private final Calendar mCalendar;

    private boolean mTitleNeedsUpdate = true;

    private View view;

    /**
     * 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);
    }
    /**
     * @param context The context the dialog is to run in.
     * @param callBack How the parent is notified that the date is set.
     * @param year The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth The initial day of the dialog.
     */
    public MyDatePickerDialog(Context context,
                              OnDateSetListener callBack,
                              int year,
                              int monthOfYear,
                              int dayOfMonth) {
        this(context, 0, callBack, year, monthOfYear, dayOfMonth);
    }

    /**
     * @param context The context the dialog is to run in.
     * @param theme the theme to apply to this dialog
     * @param listener How the parent is notified that the date is set.
     * @param year The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth The initial day of the dialog.
     */
    public MyDatePickerDialog(Context context, int theme, OnDateSetListener listener, int year,
                              int monthOfYear, int dayOfMonth) {
        super(context, theme);

        mDateSetListener = listener;
        mCalendar = Calendar.getInstance();

        Context themeContext = getContext();
        LayoutInflater inflater = LayoutInflater.from(themeContext);
        view = inflater.inflate(R.layout.date_picker_dialog, 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 = (CustomDatePicker) view.findViewById(R.id.datePicker);
        mDatePicker.setPickerMargin(25);
        mDatePicker.setDividerColor(context.getResources().getColor(R.color.sunny_theme_color_pink));
        mDatePicker.init(year, monthOfYear, dayOfMonth, this);
        //  mDatePicker.setValidationCallback(mValidationCallback);
        //实现自己的标题和ok按钮
        //setTitle("选择日期:");
        setButton();
    }

    //    private void setTitle(String title) {
//    //获取自己定义的title布局并赋值。
//        ((TextView) view.findViewById(R.id.date_picker_title)).setText(title);
//    }
    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);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        mDatePicker.init(year, month, day, this);

    }


    /**
     * Gets the {@link DatePicker} contained in this dialog.
     *
     * @return The calendar view.
     */
    public DatePicker getDatePicker() {
        return mDatePicker;
    }

    /**
     * Sets the current date.
     *
     * @param year The date year.
     * @param monthOfYear The date month.
     * @param dayOfMonth The date day of month.
     */
    public void updateDate(int year, int monthOfYear, int dayOfMonth) {
        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    }



    @Override
    public Bundle onSaveInstanceState() {
        final Bundle state = super.onSaveInstanceState();
        state.putInt(YEAR, mDatePicker.getYear());
        state.putInt(MONTH, mDatePicker.getMonth());
        state.putInt(DAY, mDatePicker.getDayOfMonth());
        return state;
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        final int year = savedInstanceState.getInt(YEAR);
        final int month = savedInstanceState.getInt(MONTH);
        final int day = savedInstanceState.getInt(DAY);
        mDatePicker.init(year, month, day, this);
    }
}

xml布局 date_picker_dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生日"
        android:gravity="center"
        android:padding="10dp"
        android:textSize="22sp"
        android:textColor="#666666"
        />
    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="#f2f2f2" />
    <com.miqulai.sunny.views.CustomDatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        android:layout_gravity="center_horizontal"
        android:calendarViewShown="false"
        />
    <View
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="#f2f2f2" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/date_picker_cancle"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="取消"
            android:textColor="#000000" />
        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="#f2f2f2" />
        <TextView
            android:id="@+id/date_picker_ok"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="确定"
            android:textColor="#000000" />

    </LinearLayout>
</LinearLayout>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值