简体   繁体   中英

Not able to generate an event when touching selected date in CalendarView on Android

I encountered an issue using CalendarView.

I want the user that is in activity1 go to the calendar in activity2 and then touch a date then go to activity3.

Currently, if the user wants to touch an already selected date (it uses today's date by default) to move on, he can't because the only event for calendar is OnDateChangeListener and I cannot get it to work with OnClick or OnTouch.

Anybody know a way to to make it work?

This is what I tried:

public class CalendarioActivity extends Activity {
private CalendarView calendar;
@Override
protected void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    calendar = new CalendarView(this);
    calendar.setOnDateChangeListener(_DateSetListener);
    calendar.setOnClickListener(_OnClickListener);
    setContentView(calendar);
}

private OnClickListener  _OnClickListener = new OnClickListener() {
      public void onClick(View v) {
        Date date = new Date(calendar.getDate());

        Intent i=new Intent(CalendarioActivity.this,SelecionarHoraActivity.class);

        i.putExtra("data",
                    new int[] {date.getDay(),date.getMonth(),date.getYear()});
        startActivity(i);}};

private OnDateChangeListener _DateSetListener = new OnDateChangeListener() {
      public void onSelectedDayChange(CalendarView view, 
                                      int year, 
                                      int month,
                                      int dayOfMonth) {

        Intent i = new Intent(CalendarioActivity.this,
                              SelecionarHoraActivity.class);
        i.putExtra("data", new int[] {dayOfMonth, month, year});

        startActivity(i);}}; 
}
this.calendarView.setOnDateChangeListener(this);

Did you try enabling the CalendarView as clickable via calendar.setClickable(true) . That would enable the click events. I don't know if it would do it for the individual dates though, but it would enable your onClickListener if I understand it correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM