我无法从 datepicker 获取日期并添加修复天数并显示在文本字段中。
我有一个两个文本框和一个按钮。我在按钮上实现 datepicker 对话框并在一个文本框中显示该日期,但我想在 datepicker 日期中添加 270 天并在 textbox2 上显示新日期。我想在 datepicker_date 中添加 270 天并在 newdate 文本字段中显示。
这是我的程序
public void setCurrentDateOnView() {
datepicker_date = (TextView) findViewById(R.id.datepicker);
newdate = (TextView) findViewById(R.id.datepicker1);
//dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
datepicker_date.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
}
public void addListenerOnButton() {
date = (Button) findViewById(R.id.date);
date.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
datepicker_date.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
//dpResult.init(year, month, day, null);
}
};
public void setCurrentDate1OnView() {
newdate = (TextView) findViewById(R.id.datepicker1);
//dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year1 = c.get(Calendar.YEAR);
month1 = c.get(Calendar.MONTH);
day1 = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
newdate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month1 + 1).append("-").append(day1).append("-")
.append(year1).append(" "));
}