gpt4 book ai didi

java - 日历 String.valueOf()

转载 作者:行者123 更新时间:2023-12-01 11:38:29 28 4
gpt4 key购买 nike

我目前正在尝试制作一个日历应用程序,但我的代码出了问题,我无法使用它。目前我制作了一个calendarView并设置了一个onSelectedDateChangeListner并且我分配了带有年、月和日的字符串。我有意将此值传递给另一个类,但即使我单击另一个日期,它仍然保留这个日期(2015.04.20)。对不起我的英语,这不是我的母语。相关代码如下:

MainActivity.java:

    mCalendarView.setOnDateChangeListener(new                   CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
String calYear = String.valueOf(c.get(Calendar.YEAR));
String calMonth = String.valueOf(c.get(Calendar.MONTH)+1);
String calDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));

Intent newEvent = new Intent(MainActivity.this, CalendarEvent.class);

newEvent.putExtra("YEAR", calYear);
newEvent.putExtra("MONTH", calMonth);
newEvent.putExtra("DAY", calDay);

startActivity(newEvent);
}
});

日历事件.java

Bundle extras = getIntent().getExtras();
if(extras != null){
mYear = extras.getString("YEAR");
mMonth = extras.getString("MONTH");
mDay = extras.getString("DAY");
}

TextView hello = (TextView)findViewById(R.id.helloworld);
hello.setText(mYear + "/" + mMonth + "/" + mDay);

最佳答案

您正在传递日历 c 的值,而不是更改日期的值。您可以通过调用“c.set()”来设置您正在使用的日历实例

mCalendarView.setOnDateChangeListener(new                   CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, month, dayOfMonth);
String calYear = String.valueOf(c.get(Calendar.YEAR));
String calMonth = String.valueOf(c.get(Calendar.MONTH)+1);
String calDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));

Intent newEvent = new Intent(MainActivity.this, CalendarEvent.class);

newEvent.putExtra("YEAR", calYear);
newEvent.putExtra("MONTH", calMonth);
newEvent.putExtra("DAY", calDay);

startActivity(newEvent);
}
});

不过,我认为您想做的就是:

mCalendarView.setOnDateChangeListener(new                   CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Intent newEvent = new Intent(MainActivity.this, CalendarEvent.class);
newEvent.putExtra("YEAR", String.valueOf(year));
newEvent.putExtra("MONTH", String.valueOf(month));
newEvent.putExtra("DAY", String.valueOf(dayOfMonth));
startActivity(newEvent);
}
});

关于java - 日历 String.valueOf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755346/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com