![](/img/trans.png)
[英]Android: to display next day's date when I click the next button and to show text views from an array
[英]How to show the next day's date from the selected date in the datepicker dialog in Android?
我目前正在頁面加載時以單獨的布局顯示今天的日期和日期以及明天的日期和日期。 但我的要求是
1)無論我從Datepicker,
什么Datepicker,
它都應顯示在第一個布局上
2)第二天應顯示第二天的日期。
我的第一個要求是正常工作,沒有任何問題。(例如),無論我選擇哪一天,它都會顯示在第一個布局上。 沒問題,但是我的第二個要求有問題。
考慮是否我在DatePicker,
上選擇了2018年5月25日DatePicker,
它按預期顯示在第一個布局上,但不是將第二天的日期顯示為2018年5月26日,而是顯示為2018年6月26日。 但是我需要它以某種方式工作,如果我在Datepicker,
中Datepicker,
2018年5月25日Datepicker,
則所選日期應在第一個布局上顯示,而第二天的緊接日期(2018年5月26日)應在下一個布局上顯示。 這應該適用於我在Datepicker.
選擇的任何Datepicker.
我哪里出問題了?
這是我的Activity.Java:
public class PrivacyPolicyActivity extends AppCompatActivity {
LinearLayout layout1, layout2, layout3, todayLayout, tomorrowLayout;
ImageView calendarImgView;
TextView todayDate;
TextView todayDay;
TextView tommmorrowDate;
TextView tommorrowDay;
TextView todayLabel;
TextView tommorrowlabel;
DatePickerDialog datePickerDialog;
Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_privacy_policy);
calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
final DateFormat dateFormat = new SimpleDateFormat("dd-MMM");
DateFormat day = new SimpleDateFormat("EEEE");
layout1 = (LinearLayout) findViewById(R.id.layout1);
layout2 = (LinearLayout) findViewById(R.id.layout2);
layout3 = (LinearLayout) findViewById(R.id.layout3);
todayDate = (TextView) findViewById(R.id.todayDate);
todayDate.setText(dateFormat.format(today));
todayDay = (TextView) findViewById(R.id.todayDay);
todayDay.setText(day.format(today));
tommmorrowDate = (TextView) findViewById(R.id.tommmorrowDate);
tommmorrowDate.setText(dateFormat.format(tomorrow));
tommorrowDay = (TextView) findViewById(R.id.tommorrowDay);
tommorrowDay.setText(day.format(tomorrow));
todayLayout = (LinearLayout) findViewById(R.id.todayLayout);
tomorrowLayout = (LinearLayout) findViewById(R.id.tomorrowLayout);
todayLabel = (TextView) findViewById(R.id.todayLabel);
tommorrowlabel = (TextView) findViewById(R.id.tommorrowlabel);
calendarImgView = (ImageView) findViewById(R.id.calendarImgView);
calendarImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
// SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
// Date date = new Date();
// String reqddate = dateandmonth.format(date);
//todayDate.setText(day + "/" + (month + 1) + "/" + year);
int cyear = datePicker.getYear();
int cmonth = datePicker.getMonth();
int cday = datePicker.getDayOfMonth();
Calendar cld = Calendar.getInstance();
cld.set(cyear, cmonth, cday);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
String strDate = format.format(cld.getTime());
todayDate.setText(strDate);
SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
String dayselected = dayfmt.format(cld.getTime());
todayDay.setText(dayselected);
int nextYear = datePicker.getYear() + 1;
int nextMonth = datePicker.getMonth() + 1;
int nextDay = datePicker.getDayOfMonth() + 1;
Calendar nextcld = Calendar.getInstance();
nextcld.set(nextYear, nextMonth, nextDay);
SimpleDateFormat tom_format = new SimpleDateFormat("dd-MMM");
String tom_date = tom_format.format(nextcld.getTime());
tommmorrowDate.setText(tom_date);
SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
String tom_day = day_fmt.format(cld.getTime());
tommorrowDay.setText(tom_day);
}
}, year, month, dayOfMonth);
datePickerDialog.show();
}
});
}
}
為什么您還以1增加Month和year?
int nextYear = datePicker.getYear() + 1;
int nextMonth = datePicker.getMonth() + 1;
int nextDay = datePicker.getDayOfMonth() + 1;
只是增加第二天。
cld.add(Calendar.DATE, 1);
解
calendarImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
// SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
int cyear = datePicker.getYear();
int cmonth =datePicker .getMonth();
int cday = datePicker.getDayOfMonth();
Calendar cld = Calendar.getInstance();
cld.set(cyear, cmonth, cday);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
String strDate = format.format(cld.getTime());
todayDate.setText(strDate);
SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
String dayselected = dayfmt.format(cld.getTime());
todayDay.setText(dayselected);
cld.add(Calendar.DATE, 1);
SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
String tom_day = day_fmt.format(cld.getTime());
tommorrowDay.setText(tom_day);
}
},year,month,dayOfMonth);
datePickerDialog.show();
}
});
在您的Datepicker
回調中,放置以下代碼以顯示第二天。
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, datePicker.getDayOfMonth());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.add(Calendar.DATE, 1);
SimpleDateFormat tom_format = new SimpleDateFormat("dd-MMM");
String tom_date = tom_format.format(calendar);
tommmorrowDate.setText(tom_date);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.