问题
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 looking to do is have these radio buttons automatically set the date picker ahead the respective amount of days (1, 2, and 7 days ahead).
I tried using the updateDate(int year, int month, int day) method to update my DatePicker when the user clicks the radio button, but nothing happens when any of the radio buttons are clicked.
Is there a specific way to change the datepicker's displayed date WITHOUT using the the ticker buttons?
Here is my code for the class:
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();
}
}
};
}
回答1:
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;
}
}
来源:https://stackoverflow.com/questions/10116455/setting-the-date-of-a-datepicker-using-the-updatedate-method