Custom DatePicker in Android?

后端 未结 4 806
臣服心动
臣服心动 2020-12-14 13:04

How to make the datepicker in spin view. Here is the image, \"enter

I want the datepi

4条回答
  •  醉酒成梦
    2020-12-14 13:34

    I have created a custom DatePicker where I want to show Months in String like Jan,Feb,Mar and secondly year is optional.

    So I have to make it from scratch.

    month_year_picker.xml

    
    
    
        
    
            
    
    
            
    
            
    
        
    
    

    MonthYearPickerDialog.java

    public class MonthYearPickerDialog extends DialogFragment {
    
        private DatePickerDialog.OnDateSetListener listener;
        private int daysOfMonth = 31;
    
        private NumberPicker monthPicker;
        private NumberPicker yearPicker;
        private NumberPicker dayPicker;
    
        private Calendar cal = Calendar.getInstance();
    
        public static final String MONTH_KEY = "monthValue";
        public static final String DAY_KEY = "dayValue";
        public static final String YEAR_KEY = "yearValue";
    
        int monthVal = -1 , dayVal = -1 , yearVal =-1 ;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Bundle extras = getArguments();
            if(extras != null){
                monthVal = extras.getInt(MONTH_KEY , -1);
                dayVal = extras.getInt(DAY_KEY , -1);
                yearVal = extras.getInt(YEAR_KEY , -1);
            }
        }
    
        public static MonthYearPickerDialog newInstance(int monthIndex , int daysIndex , int yearIndex) {
            MonthYearPickerDialog f = new MonthYearPickerDialog();
    
            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt(MONTH_KEY, monthIndex);
            args.putInt(DAY_KEY, daysIndex);
            args.putInt(YEAR_KEY, yearIndex);
            f.setArguments(args);
    
            return f;
        }
    
        public void setListener(DatePickerDialog.OnDateSetListener listener) {
            this.listener = listener;
        }
    
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            //getDialog().setTitle("Select your Birthday Date");
    
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Get the layout inflater
            LayoutInflater inflater = getActivity().getLayoutInflater();
    
            View dialog = inflater.inflate(R.layout.month_year_picker, null);
            monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month);
            yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year);
            dayPicker = (NumberPicker) dialog.findViewById(R.id.picker_day);
    
            monthPicker.setMinValue(1);
            monthPicker.setMaxValue(12);
    
    
            if(monthVal != -1)// && (monthVal > 0 && monthVal < 13))
                monthPicker.setValue(monthVal);
            else
                monthPicker.setValue(cal.get(Calendar.MONTH) + 1);
    
            monthPicker.setDisplayedValues(new String[]{"Jan","Feb","Mar","Apr","May","June","July",
                    "Aug","Sep","Oct","Nov","Dec"});
    
    
            dayPicker.setMinValue(1);
            dayPicker.setMaxValue(daysOfMonth);
    
            if(dayVal != -1)
                dayPicker.setValue(dayVal);
            else
                dayPicker.setValue(cal.get(Calendar.DAY_OF_MONTH));
    
            monthPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                    switch (newVal){
                        case 1:case 3:case 5:
                        case 7:case 8:case 10:
                        case 12:
                            daysOfMonth = 31;
                            dayPicker.setMaxValue(daysOfMonth);
                            break;
                        case 2:
                            daysOfMonth = 28;
                            dayPicker.setMaxValue(daysOfMonth);
                            break;
    
                        case 4:case 6:
                        case 9:case 11:
                            daysOfMonth = 30;
                            dayPicker.setMaxValue(daysOfMonth);
                            break;
                    }
    
                }
            });
    
            int maxYear = cal.get(Calendar.YEAR);//2016
            final int minYear = 1997;
            int arraySize = maxYear - minYear;
    
            String[] tempArray = new String[arraySize];
            tempArray[0] = "---";
            int tempYear = minYear+1;
    
            for(int i=0 ; i < arraySize; i++){
                if(i != 0){
                    tempArray[i] = " " + tempYear + "";
                }
                tempYear++;
            }
            Log.i("", "onCreateDialog: " + tempArray.length);
            yearPicker.setMinValue(minYear+1);
            yearPicker.setMaxValue(maxYear);
            yearPicker.setDisplayedValues(tempArray);
    
            yearPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                    try {
                        if(isLeapYear(picker.getValue())){
                            daysOfMonth = 29;
                            dayPicker.setMaxValue(daysOfMonth);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
    
            builder.setView(dialog)
                    // Add action buttons
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            int year = yearPicker.getValue();
                            if(year == (minYear+1)){
                                year = 1904;
                            }
                            listener.onDateSet(null, year, monthPicker.getValue(), dayPicker.getValue());
                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            MonthYearPickerDialog.this.getDialog().cancel();
                        }
                    });
            return builder.create();
        }
    
        public static boolean isLeapYear(int year) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, year);
            return cal.getActualMaximum(Calendar.DAY_OF_YEAR) > 365;
        }
    
        public static boolean isLeapYear2(int year) {
            if (year % 4 != 0) {
                return false;
            } else if (year % 400 == 0) {
                return true;
            } else if (year % 100 == 0) {
                return false;
            } else {
                return true;
            }
        }
    }
    

    And call it like

    MonthYearPickerDialog pd = MonthYearPickerDialog.newInstance(5,12, 1999);
                    //new MonthYearPickerDialog();
    
            pd.setListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
                    Toast.makeText(getActivity(),"Year : " + selectedYear + " Month :" + selectedMonth + " Day:" + selectedDay,
                            Toast.LENGTH_LONG ).show();
    
    
                }
            });
            pd.show(getFragmentManager(), "MonthYearPickerDialog");
    

    Edit

    Find a nice Example here

提交回复
热议问题