码迷,mamicode.com
首页 > 其他好文 > 详细

DatePickerDialog的使用和套用

时间:2016-05-12 22:23:31      阅读:794      评论:0      收藏:0      [点我收藏+]

标签:

日期的选择控件DatePickerDialog

1.系统自带DatePickerDialog的使用:

private Calendar calendar;

private int mYear;

private int mMonth;

private int mDay;

private static final int DATE_DIALOG_ID = 0;

private DatePickerDialog datePickerDialog;

private DatePickerDialog showDateDialog(int id){

        switch (id){

            caseDATE_DIALOG_ID:

                return newDatePickerDialog(getActivity(),onDateSetListener,mYear,mMonth,mDay);

        }

        return null;

    }

 

   DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener(){

        @Override

        public voidonDateSet(DatePicker datePicker, int y, int m, int d) {

            mYear = y;

            mMonth = m;

            mDay = d;

           updateDisPlay();

        }

    };

 

    private voidupdateDisPlay(){

       dateTextView.setText(String.format("%04d-%02d-%02d", mYear,mMonth + 1, mDay));

    }

使用时:

calendar = Calendar.getInstance();

mYear = calendar.get(Calendar.YEAR);

 mMonth= calendar.get(Calendar.MONTH);

 mDay =calendar.get(Calendar.DAY_OF_MONTH);

datePickerDialog = showDateDialog(DATE_DIALOG_ID);

datePickerDialog.show();

2.套用

public class DatePickerViewextends AlertDialog implements DialogInterface.OnClickListener, 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 mCallBack;

    private View view;


    public DatePickerView(Context context,OnDateSetListener onDateSetListener,

                          int year, intmonthOfYear, int dayOfMonth) {

        this(context, 0, onDateSetListener,year, monthOfYear, dayOfMonth);

    }


    public DatePickerView(Context context, inttheme,

                          OnDateSetListeneronDateSetListener, int year, int monthOfYear,

                          int dayOfMonth) {

        super(context, theme);

        mCallBack = onDateSetListener;

        Context themeContext = getContext();

//將設置alertdialog的settitle和setButton方法注释掉,因为下面要使用setContentView(View view)方法

//代替原有的setView(View view)方法,区别是替换的范围不同。替换后就不需要设置alertdialog的title和button了。

// setButton(BUTTON_POSITIVE,

//themeContext.getText(R.string.date_time_set), this);

// setButton(BUTTON_NEGATIVE,themeContext.getText(R.string.cancel),

// (OnClickListener) null);

// setIcon(0);

//setTitle(R.string.date_picker_dialog_title);

        LayoutInflater inflater =(LayoutInflater) themeContext

               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//注意,其实datepickerdialog就是把系统提供的日期选择器DatePicker包装一下而已,所以只需要实现自己需要的布局样式

//然后把DatePicker嵌套进去就可以了。

        view =inflater.inflate(R.layout.date_picker_dialog, null);

        view.setBackgroundColor(Color.BLUE);

        mDatePicker = (DatePicker)view.findViewById(R.id.datePicker);

        mDatePicker.init(year, monthOfYear,dayOfMonth, this);

 

       //实现自己的按钮

        setButton();

    }

 

    public void myShow() {

//自己实现show方法,主要是为了把setContentView方法放到show方法后面,否则会报错。

        show();

        setContentView(view);

    }

 

    private void setButton() {

//获取自己定义的响应按钮并设置监听,直接调用构造时传进来的CallBack接口(为了省劲,没有自己写接口,直接用之前本类定义好的)同时关闭对话框。

       view.findViewById(R.id.date_picker_ok).setOnClickListener(

                new View.OnClickListener() {

                    @Override

                    public void onClick(View v){

                        if (mCallBack != null){

                            mDatePicker.clearFocus();

                           mCallBack.onDateSet(mDatePicker,

                                   mDatePicker.getYear(),

                                   mDatePicker.getMonth(),

                                    mDatePicker.getDayOfMonth());

                        }

                        dismiss();

                    }

                });

    }

 

    @Override

    public void onClick(DialogInterfacedialogInterface, int i) {

 

    }

 

    @Override

    public void onDateChanged(DatePickerdatePicker, int year, int month, int day) {

        mDatePicker.init(year, month, day,null);

    }

 

    public DatePicker getDatePicker() {

        return mDatePicker;

    }

 

    public void updateDate(int year, intmonthOfYear, int dayOfMonth) {

        mDatePicker.updateDate(year,monthOfYear, dayOfMonth);

    }

 

    public interface OnDateSetListener {

        void onDateSet(DatePicker view, intyear, int monthOfYear,

                       int dayOfMonth);

    }

 

    @Override

    public Bundle onSaveInstanceState() {

        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(BundlesavedInstanceState) {

       super.onRestoreInstanceState(savedInstanceState);

        int year = savedInstanceState.getInt(YEAR);

        int month =savedInstanceState.getInt(MONTH);

        int day =savedInstanceState.getInt(DAY);

        mDatePicker.init(year, month, day,this);

    }

}

date_picker_dialog.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

    android:background="@color/white_bg">

    <TextView

       android:id="@+id/date_picker_title"

       android:layout_width="match_parent"

       android:layout_height="40dp"

       android:text="请选择日期"

       android:textColor="@color/text_color"

       android:gravity="center"

       android:background="@color/yellow"/>

 

    <DatePicker

       android:id="@+id/datePicker"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_gravity="center_horizontal"

       android:calendarViewShown="false"

       android:spinnersShown="true"

       android:background="@color/white_bg"/>

 

    <TextView

       android:id="@+id/date_picker_ok"

       android:layout_width="match_parent"

        android:layout_height="40dp"

       android:gravity="center"

       android:text="完成"

       android:textColor="@color/text_color"

       android:background="@color/yellow"/>

</LinearLayout>

使用时:

datePickerView = newDatePickerView(getActivity(),mDateSetListener,mYear,mMonth,mDay);

datePickerView.myShow();

//mDateSetListener类似上面的onDateSetListener

DatePickerDialog的使用和套用

标签:

原文地址:http://blog.csdn.net/nzzl54/article/details/51352384

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!