Setting the date of a DatePicker using the updateDate() method

前端 未结 1 1483
暖寄归人
暖寄归人 2021-01-22 14:04

I am currently in the process of making a date/time picker class. Essentially, I have created 3 radio buttons: \'tomorrow\', \'In 2 days\', and \'next week\'.

What I am

相关标签:
1条回答
  • 2021-01-22 14:16

    use the below modified code::::

    package com.inviscidlabs.schooled;
    
    import java.util.Calendar;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    
    public class DateTimePicker extends Activity{
    
        private static int TODAY = 0;
        private static int NEXTDAY = 1;
        private static int NEXTWEEK = 2;
    
        private DatePicker datePicker;
        private RadioGroup dtpRadGroup;
        private RadioButton rTomorrow;
        private RadioButton rIn2Days;
        private RadioButton rNextWeek;
    
    
        private Calendar cNow;
        private Calendar c;
    
    
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            //declare resources
            setContentView(R.layout.datetimepicker);
            dtpRadGroup=(RadioGroup) findViewById(R.id.dtp_radiogroup);
            rTomorrow=(RadioButton) findViewById(R.id.dtp_button_tomorrow);
            rIn2Days=(RadioButton) findViewById(R.id.dtp_button_twodays);
            rNextWeek= (RadioButton) findViewById(R.id.dtp_button_nextweek);
            datePicker= (DatePicker) findViewById(R.id.dtp_datepicker);
    
            cNow= Calendar.getInstance();
            cNow.setLenient(true);      c.setLenient(true);
            datePicker.init(cNow.get(Calendar.YEAR), cNow.get(Calendar.MONTH), cNow.get(Calendar.DAY_OF_MONTH), dListener);
    
        }
    
        public void onClick(View v){
    
            switch(v.getId()){
    
            case R.id.dtp_button_finished:
    
                break;
            }
    
        }
    
        public void onRadioButtonClicked(View v){
            c=Calendar.getInstance();
    
    
    
            switch(v.getId()){
    
            case R.id.dtp_button_tomorrow:
                 c.add(Calendar.DAY_OF_YEAR, 1);
                 showDialog(TODAY);
                // datePicker.updateDate(c.get(Calendar.YEAR),c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
    
                break;
            case R.id.dtp_button_twodays:
                 c.add(Calendar.DAY_OF_YEAR, 2);
                 showDialog(NEXTDAY);
                 //datePicker.updateDate(c.get(Calendar.YEAR),c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                break;
            case R.id.dtp_button_nextweek:
                 c.add(Calendar.DAY_OF_YEAR, 7);
                 showDialog(NEXTWEEK);
                 //datePicker.updateDate(c.get(Calendar.YEAR),c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                break;
            }
        }
    
        @Override
        protected Dialog onCreateDialog(int id) {
            final Calendar c = Calendar.getInstance();
            switch (id) {
            case TODAY:
                c.add(Calendar.DAY_OF_YEAR, 1);
            case NEXTDAY:
                c.add(Calendar.DAY_OF_YEAR, 2);
            case NEXTWEEK:
                c.add(Calendar.DAY_OF_YEAR, 7);
            }
            DatePickerDialog _date_repeat = new DatePickerDialog(this, mDateSetListener,c.get(Calendar.YEAR),c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)){
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                    {   
                         Calendar cPickerDate = Calendar.getInstance();
                            cPickerDate.set(year, monthOfYear, dayOfMonth);
                            int dayOfYear = cPickerDate.get(Calendar.DAY_OF_YEAR);
                            Log.v("day of year", String.valueOf(dayOfYear));
    
    
    
                            //automatically checks radio buttons if user manually adjust picker
                            if(dayOfYear==cNow.get(Calendar.DAY_OF_YEAR)+1){
                                rTomorrow.setChecked(true);
                            }
                            else if(dayOfYear==cNow.get(Calendar.DAY_OF_YEAR)+2){
                                rIn2Days.setChecked(true);
                            }
                            else if(dayOfYear==cNow.get(Calendar.DAY_OF_YEAR)+7){
                                rNextWeek.setChecked(true);
                            } else {
                                dtpRadGroup.clearCheck();
                            }                   
                    }
                };  
            };
            return _date_repeat;
        }
    }
    
    0 讨论(0)
提交回复
热议问题