美文网首页AndroidAndroid开发Android开发
Android DatePicker的简单自定义

Android DatePicker的简单自定义

作者: 荒诞的墨墨 | 来源:发表于2017-07-21 18:27 被阅读365次

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


    THEME_HOLO_DARK.gif

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

    THEME_HOLO_LIGHT.gif

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

    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"这个是为了不显示日历控件

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
    
     <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="5dip">
    
        <DatePicker
            android:id="@+id/datepick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:calendarViewShown="false">
    
        </DatePicker>
    
       </LinearLayout>
     </LinearLayout>

    相关文章

      网友评论

        本文标题:Android DatePicker的简单自定义

        本文链接:https://www.haomeiwen.com/subject/ykwjkxtx.html