CalendarView getDate 方法 returns 当前日期,未选择日期...我做错了什么?
CalendarView getDate method returns current date, not selected date... What am I doing wrong?
我的 calendarView 无法 return 选择日期,而是 return 某些始终指向今天的默认值。
我当然要更改在我的日历中选择的日期,它确实显示为已更改。我尝试在调试模式下检查视图,但没有找到任何东西。
我是运行这是在模拟器上,不是在真实的phone...我应该修改一些设置吗?我错过了什么重要的东西吗?因为我没有得到选定的日期,而是得到当前的日期,这真的很令人困惑。
<CalendarView
android:id="@+id/view_calendar_create_event_date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
这是从事件侦听器调用的
protected void createEvent(View view){
TextView eventNameView = (TextView) this.findViewById(R.id.createEventNameInput);
String eventName = eventNameView.getEditableText().toString();
CalendarView eventOccursOnView = (CalendarView) this.findViewById(R.id.view_calendar_create_event_date);
long eventOccursOn = eventOccursOnView.getDate();
Date temporary = new Date(eventOccursOn);
Event newEvent = new Event(eventName, "", 0, 0, eventOccursOn);
newEvent.save(view.getContext());
}
这就是我设置事件侦听器的方式
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// Create the event
EventDetailsActivity.this.createEvent(view);
// Notify the user
Snackbar.make(view, "Successfully created a new event!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
// Return to the previous activity
finish();
}
});
您需要实施 setOnDateChangeListener
long eventOccursOn;
eventOccursOnView.setOnDateChangeListener(new OnDateChangeListener() {
//show the selected date as a toast
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
Calendar c = Calendar.getInstance();
c.set(year, month, day);
eventOccursOn = c.getTimeInMillis(); //this is what you want to use later
}
});
我的 calendarView 无法 return 选择日期,而是 return 某些始终指向今天的默认值。
我当然要更改在我的日历中选择的日期,它确实显示为已更改。我尝试在调试模式下检查视图,但没有找到任何东西。
我是运行这是在模拟器上,不是在真实的phone...我应该修改一些设置吗?我错过了什么重要的东西吗?因为我没有得到选定的日期,而是得到当前的日期,这真的很令人困惑。
<CalendarView
android:id="@+id/view_calendar_create_event_date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
这是从事件侦听器调用的
protected void createEvent(View view){
TextView eventNameView = (TextView) this.findViewById(R.id.createEventNameInput);
String eventName = eventNameView.getEditableText().toString();
CalendarView eventOccursOnView = (CalendarView) this.findViewById(R.id.view_calendar_create_event_date);
long eventOccursOn = eventOccursOnView.getDate();
Date temporary = new Date(eventOccursOn);
Event newEvent = new Event(eventName, "", 0, 0, eventOccursOn);
newEvent.save(view.getContext());
}
这就是我设置事件侦听器的方式
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// Create the event
EventDetailsActivity.this.createEvent(view);
// Notify the user
Snackbar.make(view, "Successfully created a new event!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
// Return to the previous activity
finish();
}
});
您需要实施 setOnDateChangeListener
long eventOccursOn;
eventOccursOnView.setOnDateChangeListener(new OnDateChangeListener() {
//show the selected date as a toast
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
Calendar c = Calendar.getInstance();
c.set(year, month, day);
eventOccursOn = c.getTimeInMillis(); //this is what you want to use later
}
});