设定使用updateDate一个datepicker的日期()方法(Setting the date

2019-06-25 08:09发布

我目前在做的日期/时间选择器类的过程。 从本质上讲,我已经建立3个单选按钮:“明天”,“2天以后”和“下周”。

我所希望做的是有这些单选按钮自动设置未来的日期选取天各量(1,2,和7天提前)。

我尝试使用updateDate(INT年,月整型,日整型)方法,当用户点击单选按钮,但是任何一个单选按钮,单击时没有任何反应来更新我的DatePicker。

是否有更改日期选择器的显示日期,而不使用自动收报机按钮的具体办法?

这里是我的类代码:

 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 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);
             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);
             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);
             datePicker.updateDate(c.get(Calendar.YEAR),c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
            break;
        }
    }





    private DatePicker.OnDateChangedListener dListener = new DatePicker.OnDateChangedListener() {

        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();
                }

        }
    };





}

Answer 1:

使用以下修改后的代码::::

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;
    }
}


文章来源: Setting the date of a DatePicker using the updateDate() method