Get date and time picker value from a single dialog fragment and set it in EditText

后端 未结 3 882
深忆病人
深忆病人 2021-01-31 00:28

I am working on an application in which the user can set the date and time. I want to set the date and time in a single dialog fragment and set it to edit text. Is it possible t

3条回答
  •  猫巷女王i
    2021-01-31 01:03

    This is very simple:

    You have to set the Date/Time of the Dialog in the Bundle object and set that Bundle in Message object with the Message's setData method then in the activity where you want to retrieve those Date/Time values you have to make

    Here is How i am doing this:

    public class DatePickerDialog extends DialogFragment {
        private Handler dialogPickerHandler;
        private int mYear;
        private int mMonth;
        private int mDay;   
        private Bundle mBundle;
    
        /** This is the one argument constructor */
        public DatePickerDialog(Handler h) {
            /**
             * Getting the reference to the message handler instance instantiated in
             * calling activity
             */
    
            dialogPickerHandler = h;
        } // end constructor
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            mBundle = getArguments();
    
            mYear = mBundle.getInt("set_year");
            mDay = mBundle.getInt("set_day");
            mMonth = mBundle.getInt("set_month");
    
            return new android.app.DatePickerDialog(getActivity(),
                    mDateSetListener, mYear, mMonth, mDay);
        }
    
        private OnDateSetListener mDateSetListener = new OnDateSetListener() {
    
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
    
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
    
                Bundle b = new Bundle();
    
                b.putInt("set_year", mYear);
                b.putInt("set_month", mMonth);
                b.putInt("set_day", mDay);
    
                Message m = new Message();
    
                m.setData(b);
    
                dialogPickerHandler.sendMessage(m);
    
            }
        };
    
    }
    

    And in the Activity in which you want to retrieve those values you have to do the following:

     private Bundle mBundle;
        private int mtYear;
        private int mDay;
        private int mMonth;
        private Calendar your_date_object;
        private EditText your_edit_text;
        private SimpleDateFormat simpleDateFormat
    
        Handler mHandler = new Handler() {
            public void handleMessage(Message m) {
                mBundle = m.getData();
    
                mtYear = mBundle.getInt("set_year");
                mDay = mBundle.getInt("set_day");
                mMonth = mBundle.getInt("set_month");
    
    
                /** update the time on the start Date Button */
                your_date_object.set(mtYear, mMonth, mDay);
    
                your_edit_text.setText(simpleDateFormat.format(startDate
                            .getTime()));
    
    
            }
        };
    

    Make sure your initialize the simpleDateFormat, your_date_object in the onCreate method of your activity as follows:

    /** Initialize the simpleDateFormat object */
        simpleDateFormat = new SimpleDateFormat("yyyy dd, MM");
    
        /** Initialize the your_date_object */
    
        your_date_object = new GregorianCalendar();
    

    Thats it. :)

提交回复
热议问题