Android DatePicker的简单自定义

最近因为需求做了一个简单的选择日期的demo,我们可以同android自带的DatePicker控件达到选择日期简单联动的效果,用法挺简单的。
当在dialog在这包下android.app.AlertDialog;
选择这种样式super(context, AlertDialog.THEME_HOLO_DARK)
效果:


Android DatePicker的简单自定义_第1张图片
THEME_HOLO_DARK.gif

选择这种样式super(context, AlertDialog.THEME_HOLO_LIGHT);
效果:

Android DatePicker的简单自定义_第2张图片
THEME_HOLO_LIGHT.gif

当在dialog在这包下android.support.v7.app.AlertDialog是这种效果,
super(context, 0) 按钮的位置不一样了。。

Android DatePicker的简单自定义_第3张图片
android.support.v7.app.AlertDialog;.gif
   /*
    * Created by anymo on 2017/4/6.
    */

public class DialogDatePicker extends 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 OnDateSetListener listener;

     private DatePicker picker;


     /******
     * 日期选择回掉事件
    */
     public interface OnDateSetListener {

    void onDateSet(DatePicker datePicker, int year, int month,

                   int day);
    }

  public DialogDatePicker(Context context, int theme,OnDateSetListener listener, int year, int month,int day) {
 //        super(context, AlertDialog.THEME_HOLO_DARK);
 //        super(context, AlertDialog.THEME_HOLO_LIGHT);
 //        super(context,AlertDialog.THEME_DEVICE_DEFAULT_DARK);        
 //       super(context,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 
          super(context, AlertDialog.THEME_TRADITIONAL);

    this.listener = listener;
    Context themeContext = getContext();
    setButton(BUTTON_POSITIVE, "ensure", this);

    setButton(BUTTON_NEGATIVE, "cancel", this);

    setIcon(0);

    LayoutInflater inflater =(LayoutInflater)themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.dialog_datepicker, null);

    setView(view);

    picker = (DatePicker) view.findViewById(R.id.datepick);

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


    }
    @Override
    public void onClick(DialogInterface dialog, int which) {

    if (which == BUTTON_POSITIVE)
        tryNotifyDateSet();

    }

    private void tryNotifyDateSet() {

         if (listener != null) {
            picker.clearFocus();
           //月份要加1
            listener.onDateSet(picker, picker.getYear(), picker.getMonth() + 1, picker.getDayOfMonth());
         }
    }

    @Override
     public void onDateChanged(DatePicker view, int year, int 
     monthOfYear, int dayOfMonth) {

     if (view.getId() == R.id.datepick)
        picker.init(year, monthOfYear, dayOfMonth, this);
     }

 }

android:calendarViewShown="false"这个是为了不显示日历控件

 
 

 

    

    

   
 

你可能感兴趣的:(Android DatePicker的简单自定义)