How to achieve multiple datepicker functionality with two buttons and save those dates that are picked?

前端 未结 2 500
执笔经年
执笔经年 2021-01-24 06:05

I want to create an activity in which I want to have two button and multiple label. a user can click on buttons to select dates from and to. while those selected dates can be sh

相关标签:
2条回答
  • 2021-01-24 06:21

    I have created DatePickerFragment helper class, With this you can select any number of date with just single DatePickerFragment class.

    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    
        /**
         * The callback interface used to indicate the user is done filling in the date (they clicked on the 'Set' button).
         */
        public interface OnDateSetListener {
    
            /**
             * @param dialog The view associated with this listener.
             * @param year The year that was set.
             * @param monthOfYear The monthOfYear that was set.
             * @param dayOfMonth The dayOfMonth that was set.
             */
            void onDateSet(DatePicker dialog, int year, int monthOfYear, int dayOfMonth, int reqCode);
        }
    
        private OnDateSetListener mListener;
        private int reqCode;
        public void setOnDateSetListener(OnDateSetListener mListener, int reqCode){
            this.mListener  = mListener;
            this.reqCode = reqCode;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            int year;
            int month;
            int day;
            final Calendar c = Calendar.getInstance();
            Bundle bundle = this.getArguments();
            if(bundle!=null) {
                // Use the given date as the default date in the picker
                year = bundle.getInt("YEAR");
                month = bundle.getInt("MONTH");
                day = bundle.getInt("DAY");
            }else{
                // Use the current date as the default date in the picker
                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);
            }
    
            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
    
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            if(mListener!=null)
                mListener.onDateSet(view, year, monthOfYear, dayOfMonth, reqCode);
            this.dismiss();
        }
    }
    

    Use it in Activity or Fragment

    public class MainActivity extends FragmentActivity implements DatePickerFragment.OnDateSetListener {
        private static final int FROM_DATE_TAG = 2404;
        private static final int TO_DATE_TAG = 2405;
    
        @Override
        public void onClick(View view) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            DatePickerFragment fragment;
            switch(view.getId()){
            case R.id.fromDate:
                fragment = new DatePickerFragment();
                fragment.setOnDateSetListener(this, FROM_DATE_TAG);
                fragment.show(fragmentManager, "Date Picker");
                break;
            case R.id.toDate:
                fragment = new DatePickerFragment();
                fragment.setOnDateSetListener(this, TO_DATE_TAG);
                fragment.show(fragmentManager, "Date Picker");
                break;
            }
        }
    
        //If you want to update the alredy selected date, Specify it in budle. DatePickerFragment will take specified bundle date as default date.
        private void updateDate() {
            //get to date stored in SharedPrefrence
            String toDate = SharedPrefrenceUtils.getString(mContext, "FROM_DATE");
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
            Date parse = sdf.parse(toDate);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            FragmentManager fragmentManager = this.getActivity().getSupportFragmentManager();
            DatePickerFragment fragment = new DatePickerFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("YEAR", c.get(Calendar.YEAR));
            bundle.putInt("MONTH", c.get(Calendar.MONTH));
            bundle.putInt("DAY", c.get(Calendar.DATE));
            fragment.setArguments(bundle);
            fragment.setOnDateSetListener(this, FROM_DATE_TAG);
            fragment.show(fragmentManager, "Date Picker");
        }
    
        @Override
        public void onDateSet(DatePicker dialog, int year, int monthOfYear, int dayOfMonth, int reqCode) {
            if(reqCode == FROM_DATE_TAG){
                Calendar myCalendar  = Calendar.getInstance();
                myCalendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
                String dateString = formatter.format(myCalendar.getTime());
                Log.i("From Date","DATE:"+dateString);//"Result = DATE:May 24, 2015"
                //Store FROM_DATE in SharedPrefrence
                SharedPrefrenceUtils.putString(mContext,"FROM_DATE", dateString);
            } else if(reqCode == TO_DATE_TAG){
                Calendar myCalendar  = Calendar.getInstance();
                myCalendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
                String dateString = formatter.format(myCalendar.getTime());
                Log.i("To Date","DATE:"+dateString);//"Result = DATE:May 25, 2015"
                //Store TO_DATE in SharedPrefrence
                SharedPrefrenceUtils.putString(mContext,"TO_DATE", dateString);
            }
        }
    }
    

    Refer How to display SharedPreference stored data in Fragment? for SharedPrefrenceUtils Class.

    0 讨论(0)
  • 2021-01-24 06:24

    you can use Shared pref to store the last date picker value.

    0 讨论(0)
提交回复
热议问题