3

我目前正在制作日期/时间选择器类。本质上,我创建了 3 个单选按钮:“明天”、“两天后”和“下周”。

我要做的是让这些单选按钮自动将日期选择器设置在相应的天数(提前 1、2 和 7 天)之前。

当用户单击单选按钮时,我尝试使用 updateDate(int year, int month, int day) 方法更新我的 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();
                }

        }
    };





}
4

1 回答 1

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;
    }
}
于 2012-04-12T02:49:18.247 回答